Functions and Aliases
Learn about similarities and differences between functions and aliases.
We'll cover the following...
Similarity between functions and aliases
We declared the mem
function. It prints statistics on memory usage. The following alias does the same thing:
alias mem="cat /proc/meminfo"
It looks like functions and aliases work the same way. What should we choose, then?
Functions and aliases have only one aspect in common: they are both built-in Bash mechanisms. From the user’s point of view, they both shorten long commands. However, these mechanisms work in completely different ways. Let’s explore how each works.
Aliases
An alias replaces one text with another in a typed command. In other words, Bash finds a part of the command that matches the alias name. Then, the shell replaces it with the alias value. Finally, Bash executes the resulting command.
To better understand aliases, let’s suppose we declare the alias for the cat
utility. It adds the -n
option to the utility call. This option adds line numbers to the cat
output. The alias ...