Bash Aliases and Functions
Part of the book: Bash: The Linux Command Line
Aliases and functions are interesting ways to customize Bash. You can create handy short forms and simplify complex commands.
Aliases
Within Bash one can create different names for commands. Aside from simply creating a different name, the command can include options and other arguments. This is called an alias in Bash.
Creating aliases are easy. Let's create a simple alias for the ls command that does a long listing, i.e. a listing with permissions, file size and date. I type ls -l very often and it would be handy to not have to type out the whole thing every time. I know, it's only 4 characters (there's a space), but those keys can add up over time and carpel tunnel syndrome is a serious career risk for people like me.
So let's shorten it to a single simple l using an alias with this command:
alias l='ls -l'
Once set we can now use the l command and it works just like typing the whole thing out.
We can see what aliases are set using the alias command all by itself:
alias
After we have set the above alias it should look like:
alias l='ls -l'
We can use aliases like real commands providing options and arguments. When specified additional options or arguments are added to the end of the aliased command. Let's say we want to add the -h option (make file size human readable) when we run l and we also want to list a specific file:
l -h .bashrc
From the results it's as if we typed this command: ls -l -h .bashrc.
We can delete an alias using the unalias command. If we also use the -a option of this command it removes all aliases:
unalias l
Alias Scope
Aliases have a limited scope in that they are only available to the shell in which they were defined. Subshells do not see them, nor can they inherit them.
To make an alias available to all shells define the alias in your .bashrc file or in the system-wide /etc/bashrc file.
Alias Limitations
Aliases are limited in that any arguments are added to the end of the aliased command. Although we make an alias out of a complex command the arguments always go the end. So let's say we want to add paging to our l command by piping the result through less, we might try this:
alias l='ls -l | less'
When we use the l command without an alias it works as expected but as soon as we add a file name it, say the same .bashrc. When we try this we see the .bashrc file listed to the screen. That's because the actual command executed is ls -l | less .bashrc
Because of this limitation we can't do many other neat things. This is where we can use functions.
Functions
Functions are more powerful than aliases. Not only can we re-arrange parameters but we can actually create simple programs using them.
The topic of functions really becomes a programming lesson quickly so I'll only go into a few simple examples here to solve the limitations of alias.
Let's say we want to achieve the last failed example, we want to page the output of ls -l. we would write a function like this:
unalias l l() { ls -l "$@" | less; }
I've used separate lines to write this function but it can all be put on a single line if that's more convenient. It's proper programming style to have it on seprate lines.
When you type this in you'll notice that you get a different prompt after the first line. This is the PS2 prompt that we talked about in the Bash Environment Variables page. This prompt means that the command is not complete. Once you type the closing brace (}) you will be back to the normal prompt.
Notice that I've also deleted the l alias. This is because both cannot exist at the same time.
With the l function created you can now use it like a command. Feel free to create more complex multi-line functions that do very complex things if you like.
To see the functions you've created run the set command. The functions will listed after the environment variables.
Functions can be deleted using the unset command:
unset l
Function Scope
Functions are also local to the instance of bash in which they are created. To make it available to sub shells add it to your .bashrc file or the system-wide /etc/bashrc file.