Brevis esse laboro, Obscurus fio -- I strive to be brief, and I become obscure.     CEE UCL HelpDesk Homepage

Department of Civil and Environmental Engineering
Frank Batten College of Engineering and Technology
Old Dominion University
Norfolk, Virginia 23529-0241, USA
Tel) (757) 683-3753
Fax) (757) 683-5354


	
Return to CEE UCL HelpDesk Homepage
CEE UCL HelpDesk Access Counter
 
How do I assign alias(es) to my Z Shell (zsh)?


  


  1. Syntax for Defining Alias
  2. Bourne Again shell (bash) alias examples
  3. Managing Alias(es)




Syntax for Defining Alias
  
Alias is a pseudonym or shorthand for a command or series of commands, i.e., a convenient macro for frequently used command or a series of commands. An alias definition affects the current shell execution environment and the execution environments of the subshells of the current shell. The alias definition will not affect the parent process of the current shell nor any utility environment invoked by the shell.

For Z shell (zsh), you can define alias(es) using

alias alias_name='command'

syntax in .zshrc file in your root directory. You can define as many aliases as you want (as long as you can remember alias names) in .zshrc.

Z shell (zsh) alias examples
  

Collection of useful aliases

For example, to create a simple command alias, first open your .zshrc file in your home directory using any text editor of your choice.

alias del='rm -i'

This creates an alias del for the command "rm -i" which prompts you for confirmation that you want to remove a file before it does so.

To create a command alias that consists of a series of commands,

alias llm='/usr/bin/ls -Flsa $1'

This creates an alias llm for the command "ls" with "Flsa" flags on a user-given directory name (=$1).

Of course, you can refer to another command alias within an alias,

alias h=history
alias rev='h | tail -10'

The first command assigns an alias h to the "history" command. The next command assigns another alias rev to the command "h | tail -10". This takes the output from the alias h (= the "history" command) and pipes it through the "tail" command to list the ten most recent commands in the command history.

Here's an example for using more than one alias on the same command line,

alias root='cd /; '
alias slist='ls -l | head -5'

Provided the last character in the root alias definition is a blank space (" "). Thus any argument to this alias as also checked to see if it is an alias. If so, it is executed. Also, notice that in the first alias definition the command ends in a ";" (semicolon) to allow another command to follow it.

If you type these two aliases, root slist at the command prompt, then you're changing to your system root directory and its contents is listed in a long format with only the first five lines of output being displayed.

Here an example to pass command arguments to the alias,

alias print='lpr $1 -Php4'

Now, to print a file to the CEE UCL default printer "hp4," you can enters a command such as:

$ print ohboy.f

The notation "$1" causes the first argument to the command alias print to be inserted in the command at this point. The command that is carried out is:

lpr ohboy.f -Php4

Of course, you can also pass multiple arguments to a command alias.

alias mprint='lpr $* -Php4'

The notation "$*" causes multiple arguments given to the alias mprint to be inserted in the command. If you enters following command

$ mprint ohboy.f headache.txt huge_bug_list.ps

the actual command would look like

lpr ohboy.f headache.txt huge_bug_list.ps -Php4

Managing Alias(es)
  

  1. To display the value of an alias known to the shell, type;

    $ alias

    To display the current value for a particular alias, use the command;

    $ alias name_of_alias

    For example, to display the current value of the alias named llm;

    $ alias llm
    llm='/usr/bin/ls -Flsa $1'

  2. Once alias definition is save in .bashrc, the alias will be in effect next time you login. If you wish to make the alias in effect immediately, update your alias definition by sourcing out .zshrc.

    $ . .zshrc

  3. To cancel a command alias during your current login session use the command;

    $ unalias name_of_alias

    For example,

    $ unalias llm

    will cancel the command alias llm for your current login session. You will have to remove the alias definition from your .zshrc file if you want to cancel the alias permanently.

For further details, RTFM on "alias."


Return to CEE UCL HelpDesk Homepage Move to the Top of this page