alias

Create an alias. Aliases allow a string to be substituted for a word. An alias can 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.

The value of an alias can be set to an expression including spaces, command options and/or variables, the expression must be quoted, either with 'Single quotes' that will be evaluated dynamically each time the Alias is used, or "Double quotes" which will be evaluated once when the alias is created.

The value cannot contain any positional parameters ($1 etc), if you need to do that use a shell function instead.

The name can not be 'alias' or 'unalias'.

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 can not be 'alias' or 'unalias'.

unalias can 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 you can alias ls to ls -F, for instance, and bash does not try to recursively expand the replacement text.

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' and 'unalias' are BASH built-ins. For almost every purpose, shell functions are preferred over aliases.

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 l="ls -l"      #Long
$ alias la="ls -la"    #Long + show all
$ alias ll='ls -lahL'  #Long + show all + follow symlinks w. dest

$ alias ls-al='ls -al' #fix missing space typo

Create an alias cp to ensure that when copying files, progress is always displayed and files do not get overwritten without a confirmation:
$ alias cp="cp -iv"

Create an alias mv to ensure that when moving files, progress is always displayed and files do not get overwritten without a confirmation:
$ alias mv="mv -iv"

Use alias with cd 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 https://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 >

“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 macOS commands:

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


 
Copyright © 1999-2020 SS64.com
Some rights reserved