Understanding shortcomings with aliases

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 ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.