Code Done Right!

Shell alias

If you are using Bash then, most likely, you have some commands or a script that you run quite often, alias is the way to go about it. For me that would be a script called cdr_backup.sh that helps me make a backup of this server.

Now, if you are running such a script with any frequency whatsoever it would be inconvenient to type the path to said script every time you need to run it.

Let me give you an example – date is a command to print or set the system date and time, and the actual command points to /usr/bin/date. To get the current date without an alias you would have to type /usr/bin/date every time, but since there is a convenient alias in place you can just type date and the result is the same.

Try the following three commands

which date
/usr/bin/date
date
  • The first one shows us where the date command is located in the system
  • The second one is the absolute path to that command and executes it
  • Third one is an alias to the command

This is the power of an alias. Moreover, aliases can be used in script as well.

Creating an alias

Creating an alias is a very simple process. Take a look at ls command. It lists contents of a directory. In your terminal type

ls ~

You should see the contents of your Home directory. Now type

ls -la ~

You will be presented with a more detailed list of files since -la arguments changes the behavior of ls command to show output as a list and include all files, even if they are hidden.

I always use -la flags so instead of typing ls -la <directory> I created an alias so that every time I type ls it actually processes ls -la instead. In order to do that type

alias ls="ls -la"

Now list your Home directory again by using

ls ~

The result should be the same as ls -la, but you might have lost color hints. That is expected. Do not worry this alias is temporary.

Take a look at the syntax of the following

alias $1="$2"
  • alias – this command tells the shell that you want to create an alias
  • $1 – denotes what alias you want to use, put a string of text here that you want to type in bash to get the result from $2
  • $2 – this is a command you are making alias of, here you can put anything that you want executed. A command with additional arguments, a path to a shell script etc.

If you want to make an alias that will show all files in your Home folder just by typing show_home you can do that with

alias show_home="ls -la ~"

Now every time you type show_home, shell will execute ls -la ~ It is not the most useful example but an easy one to understand.

You can check aliases set on your system by simply typing alias in your terminal without any additional arguments. Try it to check what aliases you have that you might not know of, and at the same time you will confirm that show_home is recognized by your shell.

Removing an alias

This one is even simpler, to remove the alias that you just created simply type

unalias show_home

Now if you type show_home in your terminal is should simply say

show_home: command not found

Your alias is gone. Confirm by typing alias in your terminal

Temporary vs permanent alias

If you set an alias in your shell by using the alias command it is just a temporary alias. If you log out it is gone. Try it out, make an alias, relog and try using it again. It is not there anymore.

If you want to make a permanent alias you have to include it in ~/.bashrc file. Each user has its own file and any changes will affect only that user. If you set up an alias on one account it will not work on the other.

Creating a permanent alias

Now try adding show_home as a permanent alias, use the following command to modify your .bashrc file

nano ~/.bashrc

Navigate to the end of the file and place the following inside

alias show_home="ls -la ~"

Save and close the file and issue the command to re-read the contents of the file

source ~/.bashrc

Alternatively you can just relog, but sourcing is much more convenient.

Now you have a permanent alias that will be available every time you log in.

Removing a permanent alias

As you probabily figured that out you can just remove the line with your alias from the .bashrc file, source it again and it is gone. I am making a note for the completeness of the tutorial.

Another way of adding a permanent alias

You can also insert your alias by appending the .bashrc file from the command line like so

echo "alias show_home='ls -la ~'" >> ~/.bashrc

The echo command redirects its standard output to append the .bashrc file. Note that I used single quotes for the command to be aliased, and double quotes for the full line that is supposed to be added.

Caution! Remember that >> will add to the file, however, > will substitute the file contents. Read more about redirecting a command output for more information. In essence > will destroy your file, >> will add to your file.

What else can you do with an alias?

In the first paragraph I mentioned cdr_backup.sh script that I run quite often. If I were to run it manually everytime I want to make a backup I would have to type

/var/zzz_script/cdr_backup.sh

every time, however, I set up an alias so that I can just type cdr_backup in terminal and the script is executed. The entry in .bashrc file looks like so

alias cdr_backup='/var/zzz_script/cdr_backup.sh'

You can check out the script here.

Conclusion

Bash helps automating tasks and alias is just one more way to do so. It is also more convenient to use if you have a dedicated folder with script that you use with any frequency, or if you use certain commands with additional arguments all the time.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.