Aliases are simple to understand, but they have some shortcomings as a means of writing your own commands. One of the biggest problems is that they have no way of modifying their behavior based on any arguments given to them; they are just simple text substitutions.
For example, suppose you wanted to make an alias, mkcd, that created a directory before changing into it—a very useful shortcut! If you wanted to do this with aliases, you might try to define it like this, separating each command with a semicolon:
bash$ alias mkcd='mkdir -p;cd'
But this doesn't work:
bash$ mkcd createme mkdir: missing operand Try 'mkdir --help' for more information. -bash: cd: createme: No such file or directory
If we use set -x again, we can see the actual command Bash ran, and see what went wrong:
bash$ set -x bash$ mkcd createme + mkdir -p mkdir: missing operand Try 'mkdir --help' for more information. + cd createme bash: cd: createme: No such file or directory bash$ set +x
The createme argument was only passed to the last command, cd; what Bash saw and executed was mkdir -p;cd createme. We need a way to pass the same argument to both commands. This attempt using $1 to get the first argument doesn't work either:
bash$ alias mkcd='mkdir -p -- "$1" && cd -- "$1"' bash$ mkcd createme mkdir: cannot create directory ‘’: No such file or directory -bash: cd: too many arguments
With aliases, there is no practical way to handle this simple requirement. Functions can do this, however.
Another problem with aliases is that invoking them in Bash scripts doesn't work; they are strictly for use in interactive mode. Worse, there is no error raised when they're defined—they just don't work when called, so it's hard to tell what went wrong:
bash$ cat alias.bash alias ll='ls -l' ll bash$ bash alias.bash line 2: ll: command not found
To make it even more complicated, in some other shells, aliases do work in scripts. It's better to avoid the whole mess by always using functions instead, which work the same way in all POSIX family shells.
A third problem is that because the commands are in strings, escaping the characters or using quotes in the command definitions can get very confusing:
bash$ alias mybook='cd ~/'\''User'\''s Guide'\'
To summarize, our advice is to avoid aliases; they have no practical advantages over functions or scripts. The preceding discussion will help you edit aliases in other people's scripts that you may have to maintain—but if you can replace them with functions, do it! You will thank yourself later. Even the bash manual itself agrees: