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.
Unalias will remove an alias.

Syntax
      alias [ {+|-}gmrsL ] [ name[=value] ... ]

Key
   -g   Define a global alias
        global aliases are expanded even if they do not  occur in command position.

   -L   Print each  alias in a manner suitable for putting in a startup script.

   -m   Arguments are taken as patterns, they should be quoted to  preserve them from being
        interpreted as glob patterns, the aliases  matching these patterns are then printed.

   -s   Define a suffix alias.

Syntax
      unalias [ -ams ] name ...

Key
   -a   Remove all regular or global aliases.
   -m   Remove by pattern.
   -s   Remove all suffix aliases, in this case no name arguments may appear.  .

'alias' and 'unalias' are ZSH built-ins, see also the bash alias built in.

For almost every purpose, shell functions are preferred over aliases.

For each name with no value, print the value of name, if any. With no arguments, print all currently defined aliases other than suffix aliases. If the -m flag is given the arguments are taken as patterns (they should be quoted to preserve them from being interpreted as glob patterns), and the aliases matching these patterns are printed. When printing aliases and one of the -g, -r or -s flags is present, restrict the printing to global, regular or suffix aliases, respectively; a regular alias is one which is neither a global nor a suffix alias. Using ‘+’ instead of ‘-’, or ending the option list with a single ‘+’, prevents the values of the aliases from being printed.

If the -L flag is present, then print each alias in a manner suitable for putting in a startup script.

The exit status is nonzero if a name (with no value) is given for which no alias has been defined.

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.

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:

If you have the ZSH shell, then use a text editor to edit or create a file called ~/.zshrc and add your alias commands.

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

Local man page: alias - Command line help page on your local machine.
env - Display, set, or remove environment variables.
echo - Display message on screen.
set - Set a shell variable.
shift - Shift positional parameters.


 
Copyright © 1999-2024 SS64.com
Some rights reserved