InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Shortening or Changing Command Names
Bash

Shortening or Changing Command Names

InfinityCoder April 5, 2017

You’d like to shorten a long or complex command you use often, or you’d like to rename a command you can’t remember or find awkward to type.

Do not manually rename or move executable files, as many aspects of Unix and Linux depend on certain commands existing in certain places; instead, you should use aliases, functions, and possibly symbolic links.
According to the bash Reference, “Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.

The shell maintains a list of aliases that may be set and unset with the alias and unalias built-in commands.”

This means that you can rename commands, or create a macro, by listing many commands in one alias.

For example, alias copy=’cp’ or alias ll.=’ls -ld .*’. Aliases are only expanded once, so you can change how a command works, as with alias ls=’ls -F’, without going into an endless loop.

In most cases only the first word of the command line is checked for alias expansion, and aliases are strictly text substitutions; they cannot use arguments to themselves.

In other words, you can’t do alias=’mkdir $1 && cd $1′ because that doesn’t work.
Functions are used in two different ways. First, they can be sourced into your interactive shell, where they become, in effect, shell scripts that are always held in memory.
They are usually small, and are very fast since they are already in memory and are executed in the current process, not in a spawned subshell.

Second, they may be used within a script as subroutines.

Functions do allow arguments. For example:

1
2
3
4
5
6
7
8
# cookbook filename: func_calc
 
# Trivial command line calculator
function calc {
   # INTEGER ONLY! --> echo The answer is: $(( $* ))
   # Floating point
   awk "BEGIN {print \"The answer is: \" $* }";
} # end of calc

For personal or system-wide use, you are probably better off using aliases or functions to rename or tweak commands, but symbolic links are very useful in allowing a command to be in more than one place at a time.

For example, Linux systems almost always use /bin/bash while other systems may use /usr/bin/bash, /usr/local/bin/bash, or /usr/pkg/bin/bash.

While there is a better way to handle this particular issue, in general symbolic links may be used as a workaround.

We do not recommend using hard links, as they are harder to see if you are not looking for them, and they are more easily disrupted by badly behaved editors and such.

Symbolic links are just more obvious and intuitive.

Usually, only the first word of a command line is checked for alias expansion.

However, if the last character of the value of that alias is a space, the next word will be checked as well.

In practice, this is rarely an issue.
Since aliases can’t use arguments (unlike in csh), you’ll need to use a function if you need to pass in arguments. Since both aliases and functions reside in memory, this is not a big difference.
Unless the expand_aliases shell option is set, aliases are not expanded when the shell is not interactive.

Best practices for writing scripts dictate that you not use aliases, since they may not be present on another system.

You also need to define functions inside your script, or explicitly source them before use.

Thus, the best place to define them is in your global /etc/bashrc or your local ~/.bashrc.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using or Replacing Built-ins and External Commands in bash
You want to replace a built-in command with your own …

Using or Replacing Built-ins and External Commands in bash

Changing Behavior with Redirections in bash
Normally you want a script to behave the same regardless …

Changing Behavior with Redirections in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap