Friday, October 10, 2014

How to create a persistent alias in Bash

One thing I've come to love about Linux is that it has an intelligent command prompt. In fact, there is more than one command prompt we call the shell. But the one I use and the one that is most popular is the Borne Again Shell, also known as Bash.

I use Bash on a daily basis. When I first started to use Bash, I studied howto articles on the web to learn how to use it. What I have learned is that Bash has many convenient features that the Windows command prompt was missing. Like a persistent memory. At the bash prompt, enter the command, history, and you will see the last 500 commands that you have entered before. That number can be adjusted, too. Windows doesn't have that in their command prompt. Yes, they do have Powershell, but that borrows many things from Linux.

I use Bash to get simple things done every day, like opening a set of files that I use often. To make it easy to run that command, I've created an alias that is just a few characters long and that makes it easy for me to repeat common tasks. Today, I want to show you how to create an alias in Linux.

I like to update my computer often. On a weekly basis, I find that there are updates for programs on my computer, big and small. Some are very important, some not so much. But keeping my computer up to date on Linux is an important task, just like on Windows. I do this for security and for bug fixes, just like on Windows.

Normally, when I run updates, I run the following commands:

scott@scott_machine:~$ sudo apt-get update
scott@scott_machine:~$ sudo apt-get upgrade

The first command updates the software database, a database that tracks the software installed on my computer. The second command downloads and installs the updates. Sudo is a command that allows me to temporarily run as root to run commands that make changes to the system and requires a password to run.

When I first started doing updates via command line, I was happy to enter the commands and watch the text crawl up the screen during the update and upgrade process. It's a lot more transparent than using the GUI, the point-and-click way. But after awhile I got lazy and started hunting for the commands in my history:

scott@scott_machine:~$ history
692  df -h
693  exit
694  sudo reboot
695  sudo apt-get update && sudo apt-get upgrade

Once I found the last update in history, I could rerun that command:

scott@scott_machine:~$ !695

But even that was getting a little tiresome. So I did some research and found out how to make an alias and make it persistent. You could probably do this with Windows, too. But I bet it's harder to do there. So here we go. At the prompt, type alias:

scott@server:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias chex='~/'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

That tells you what aliases are already recognized as present in your user profile. Every user on a Linux system has a profile. That can be found in the file, .bashrc. To find it, you will need to list all the hidden files as well as normal files. The dot before the filename for .bashrc means that it's a hidden file. So that when you normally list the directory contents, they're hidden. But if you run the command, ls -al, all the contents will be listed, including the hidden files. Notice that in the example above, there are several aliases defined for ls. The one I use the most is ll, the second from the bottom. These aliases can be modified to your liking, too. But today, we're going to focus on adding an alias.

First, decide on what alias we want to add. I wanted to add one for updating my computer, so I chose "updt", a short and sweet string to enter when I want to check for updates and install them. To add the alias, I just modify the .bashrc file like so:

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias chex='~/'
alias updt='sudo apt-get update && sudo apt-get upgrade'

See that last line? That's the one that I added. In Ubuntu, the default .bashrc file will have a section for more aliases as shown above. To edit the file, run the following command: 

scott@server:~$ vi .bashrc

Then you can add the alias as shown above or however you like, but you follow the syntax shown above. Vi is the command line text editor in Linux. You can learn more about Vi here. The line starts with alias, followed by the new command you want to use, which is arbitrary, but should not be the same as another command that already exists. You can test it by entering the new command without an alias to see if anything happens. You should get a "command not found" message with your new command, if you do, then you can proceed. If not, try a new string until you find one you like. It won't take long.

In each line that defines an alias, the string you want to use is defined as a variable, followed by the equals sign. After the equals sign, you enclose the command(s) you want to use in single quotes. Note that for more than one command to be run, as in my example, I use two ampersands. That allows more than one command to run in sequence.

Once you finish adding the new alias, you must log out and log back in again for the new alias to take effect. You can check to see if it's there by typing alias at the prompt again. If it's there, then run it.

That's it. I hope you enjoyed this little primer on aliases in Bash. Have fun with it.
Post a Comment