Project51.Define Shell Aliases


Project 51. Define Shell Aliases

"How do I define a shortcut for a commonly used command line?"

This project explores Bash aliaseshow to define them, list them, and undefined themand shows you a few neat alias tricks. The last section covers Tcsh shell equivalents. It introduces the commands alias and unalias. Project 52 covers Bash functions.

Note

A Bash alias is not the same as a Finder alias.


Tip

You can expand an alias before it is executed by typing Escape Control-e before pressing Return.


Define an Alias

Bash supports the definition of aliases. An alias can be defined to be a complete or partial command line. When an alias is used as the first word on a command line, Bash expands the alias by replacing it with its definition. Note that the alias must be the first word on the command line; otherwise, it will not be recognized as an alias and will not be expanded.

Learn More

Project 80 teaches you the intricacies of shell quoting.


Here's an alias that creates a shortcut, called ll, for the command line ls -lt. Although doing so is not necessary in this example, it's customary to enclose the definition of an alias in single quotes to ensure that the shell doesn't interpret it before it's assigned.

$ alias ll='ls -lt'


Typing ll on the command line issues the command ls -lt.

$ ll drwx------ 36 saruman saruman 1224 Jul 15 18:50 Library drwx------  4 saruman saruman  136 Jul 15 08:44 Desktop drwxr-xr-x  5 saruman saruman  170 Jul  2 15:03 Public ...


An alias need not form the complete command line. For example, it will accept arguments and option settings that work with the command it's based upon. We specify arguments after the alias name, and they are added to the end of the expanded command line.

$ ll Sites total 16 drwxr-xr-x  5 saruman saruman  170 Jul 16 11:54 images -rw-r--r--  1 saruman saruman 5754 Jun 23 11:59 index.html


Note

Bash expands aliases before it expands most other syntax elements, such as tilde ( ~ ) and $VARIABLE. This means that aliases' definitions may rely on most shell expansions.


Aliases in Aliases

Bash allows you to use an existing alias in the definition of a new one so long as it's the first word in the definition. To define a shortcut to "long list" your home directory with the previously defined alias ll, type

$ alias lh='ll ~'


Tip

Make an alias permanent by writing it to a Bash configuration file. See Project 47.


When the command line is interpreted by Bash, lh expands to ll ~, which in turn expands to ls -lt ~ and ultimately, through tilde expansion, to ls lt /Users/yourhomedir.

List and Remove

To recall the definition of an alias, use its name as an argument to alias.

$ alias ll alias ll='ls -lt'


Used without options or arguments, the alias command displays the definitions of all aliases. To remove an alias you've created, use the unalias command.

Alias Expansion Quirks

Bash expands aliases when a command line is read, not when it is executed. In the example below, in which we have two commands on the same line (separated by a semicolon), the alias d becomes defined only upon execution of the command and, therefore, is not available for expansion when the second command on the linethe supposed expansion of alias dis parsed.

$ alias d="cd ~/Documents"; d -bash: d: command not found


Issuing the command d will work immediately after the defining line has executed.

$ d $ pwd /Users/saruman/Documents


The Double-Alias Trick

Earlier in this project, we learned that an alias must be the first word on the command line; otherwise, it won't be recognized as an alias and won't be expanded. Let's illustrate this by typing the following sequence of commands.

$ alias ls='ls -l' $ alias d='~/Documents' $ ls d ls: d: No such file or directory


We can employ a trick to make Bash expand d even though it's not the first word on the command line. An alias definition that ends with a space character signals Bash to consider the next word for expansion, too. Let's redefine the alias but add a space to the end.

$ alias ls='ls -l '


This time, both the aliases ls and d will be expanded.

$ ls d drwxr-xr-x 4 saruman saruman 136 Jun 15 12:39 AppleW... drwxr-xr-x 11 saruman saruman 374 Jun 24 16:45 Downloads drwx------ 95 saruman saruman 3230 Jul 14 19:48 iChats ...


We could also define an alias for the cd command to take advantage of our directory shortcut.

$ alias cd="cd "


Aliases in Scripts

Here's a gotcha that might catch you out one day: You can define and use an alias in a shell script, but it won't be expanded. We can change this behavior, however, by changing the shell option expand_aliases. In an interactive shell, the option is on by default, as we can see by typing

$ shopt | grep alias expand_aliases on


Note

Shell scripts usually do not employ aliases. Functions are the preferred choice.


For a noninteractive shell (as launched to run a shell script), alias expansion is off by default.

$ cat script shopt | grep alias $ ./script expand_aliases off


To switch on alias expansion, simply include the following command in the script file.

shopt -s expand_aliases


Learn More

Project 45 covers Bash shell options and attributes in general, showing you how to display them and switch them on and off. Project 47 briefly explains the difference between interactive and noninteractive shells.


Arguments to Aliases

Aliases do not take arguments, although you can include arguments on the command line after the alias name. A chief difference between aliases and functions (covered in Project 52) is that a function followed by arguments can expand as a command sequence with the arguments inserted into the middle of the sequence.

Tcsh Shell Aliases

The Tcsh shell supports aliases but uses a slightly different syntax. We define an alias by typing a command such as

% alias ls 'ls -l'


The Tcsh shell does not support functions, but its aliases can take arguments and have them inserted into the middle of the expanded sequence.

Let's define an alias e to demonstrate this. For illustrative purposes, our alias simply echoes its arguments in the reverse order to which they are given and then echoes all arguments. The sequence !:n expands to argument number n, and the sequence !:* expands to all arguments.

% alias e 'echo arg2=\!:2 arg1=\!:1 all=\!:*' % e one two arg2=two arg1=one all=one two


Note

In the Tcsh shell, you must escape the ! character, even when it is placed inside single quotes.





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net