Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

Functions and Aliases in bash

  • Print
  • + Share This
Addicted to aliases? Maybe you should be investigating functions, says Pat Eyler. Functions can do a lot more than aliases--flow control, error handling, using variables. And functions can be reused in scripts, while aliases can't.
Pat Eyler is the author of Networking Linux: A Practical Guide to TCP/IP (New Riders, 2001, ISBN 0-7357-1031-7).
Like this article? We recommend

Many tutorials and introductions to bash talk about using aliases. Unfortunately, most of them don't cover functions. This is a real loss, because functions offer many values that aliases don't.


Aliases are simple string substitutions. The shell looks at the first word of a command and compares the command against its current list of aliases. Further, if the last character of an alias is a space, it looks at the next word as well. For example:

$ alias 1='echo '
$ alias 2='this is an alias'
$ 1 2
this is an alias

Aliases don't allow for control flow, command-line arguments, or additional trickery that makes the command line so useful. Additionally, the rules surrounding alias expansion are a bit tricky, enough so that the bash(1) man page recommends "To be safe, always put alias definitions on a separate line, and do not use alias[es] in compound commands."

  • + Share This
  • 🔖 Save To Your Account