alias

Create an alias. Aliases allow a string to be substituted for a word. An alias may be used as the first word of any simple command.

Syntax
      alias [-p] [name[=value] ...]

      unalias [-a] [name...]

Key
   -p   Print the current values

   -a   Remove All aliases

When arguments are supplied, an alias is defined for each name whose value is given. A trailing space in value causes the next word to be checked for alias
substitution when the alias is expanded.

If no value is given, (or with the -p option) alias will print a list of Aliases along with their current values. For each name in the argument list for which no value is supplied, the name and value of the alias is printed.

Alias returns true unless a name is given for which no alias has been defined.

name may not be `alias' or `unalias'.

unalias may be used to remove each name from the list of defined aliases.

Alias substitution
The shell maintains a list of aliases which can be set, unset and printed by the alias and unalias commands. After a command line is parsed into simple commands the first word of each command, left-to-right, is checked to see if it has an alias. If so, the first word is replaced by the alias. If the alias contains a history reference, it undergoes History substitution (q.v.) as though the original command were the previous input line. If the alias does not contain a history reference, the argument list is left untouched.

Thus if the alias for ls were ls -l the command ls /usr would become ls -l /usr, the argument list here being undisturbed. If the alias for lookup were grep !^ /etc/passwd' then lookup bill would become grep bill /etc/passwd.

Aliases can be used to introduce parser metasyntax. For example, alias print 'pr \!* | lpr' defines a command (print) which pr's its arguments to the line printer.

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second
time. This means that one may alias ls to ls -F, for instance, and bash does not try to recursively expand the replacement text.

Examples

Create an alias 'c' that will clear the screen:
$ alias c='clear'

Create an alias 'ls' that will change the default action of ls:
$ alias ls='ls --classify'
$ ls
$ unalias ls

More aliases for ls:
$ alias la='ls -lAXh --color=always'   #Show all, sort by extension
$ alias ls-al='ls -al'   #fix typo missing space
$ alias l="ls -l"
$ alias la="ls -la"

Use alias to fix missing space typos:
$ alias cd..='cd ..'
$
alias ..='cd ..'

Display the working directory
$ alias .='echo $PWD'

Prevent accidental deletions by making rm interactive:
$ alias rm='rm -i'

Shorten apt-get installation commands:
$ alias canhaz='sudo apt-get install'

Run firefox and open a specific website:
$ alias fftr='/Applications/Firefox.app/Contents/MacOS/firefox-bin http://ss64.com'

Produce a custom prompt to display which machine you are on, the current folder, and the number of the current command:

   $ alias cd='cd \!*; set currDir = `basename $cwd`; set currDir = `echo
   "<${host}:"$currDir
   " ! >"`; set prompt = "${currDir} "'
   $ cd $cwd

<Mac_One:Work-folder 15 >

Making an alias permanent:

Assuming you have the BASH shell, then use your favorite text editor to edit or create a file called ~/.bash_aliases and add your alias commands.

alias is a bash built-in command.

“There are many reasons why novelists write, but they all have one thing in common - a need to create an alternative world” ~ John Fowles

Related:

Alias man page - Apple.com
env - Display, set, or remove environment variables
echo - Display message on screen
set - Set a shell variable
shift - Shift positional parameters


© Copyright SS64.com 1999-2014
Some rights reserved