Frequently Asked Question

What's the difference between an alias and a function?

An alias is a one-line textual substitution. alias ll='ls -lh' tells bash that when ll appears at the start of a command, replace it with ls -lh and carry on. Aliases are quick to write and read, but they're limited: they don't take arguments in a flexible way (everything you type after the alias name just gets tacked on), they aren't inherited by child shells or scripts, and they're not expanded inside scripts at all by default.

A function is a real block of shell code with a name. mkcd() { mkdir -p "$1" && cd "$1"; } defines a function that takes an argument, uses control flow, and can do anything bash can do. Functions accept named arguments ($1`, `$2, $@), can have local variables, can return exit codes, and can be exported to subshells with export -f. The rule of thumb: if you only want to shorten or pre-flag a command, use an alias; if you need arguments anywhere except the end, conditional logic, or multiple commands, write a function.

Video

Further reading and video