Aliases

Among the features missing from sh was the ability to easily make new commands or modify existing commands. bash has the ability to set an alias for commonly used commands or sequences of commands. For example, if you habitually call for the older pager more but actually prefer less, an alias can be handy to get the desired behavior, regardless of the command you use:

$ alias more='less'

This has the effect of intercepting any command entries for more, substituting less. The revised command is passed along to the shell’s command interpreter.

Another common use for an alias is to modify a command slightly so that its default behavior is more to your liking. Many people, particularly when operating with superuser privileges, will use this alias:

$ alias cp='cp -i'

With this alias in effect, the use of the cp (copy) command becomes safer, because with the -i option always enforced by the alias, cp prompts you for approval before overwriting a file of the same name. Additional options you enter on the command line are appended to the end of the new command, such that cp -p becomes cp -i -p, and so on.

If the righthand side of the aliased command is bigger than a single word or if it contains multiple commands (separated by semicolons, bash’s command terminator), you probably need to enclose it in single quotation marks to get your point across. For example, suppose you wished to use a single alias to pair two simple commands:

$ alias lsps=ls -l; ps

Your current bash process will interpret ...

Get LPI Linux Certification in a Nutshell, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.