The Alias Command

The alias command allows you to create your own names or abbreviations for commands by performing string substitution on the command line according to your specifications. Aliases are recognized only by the shell that invokes them; spawned processes do not "inherit" them.

Never use the actual command syntax as an alias for itself. If for some reason an error occurs and the login file which defines your aliases doesn't run, UNIX executes the standard version of the command. Normally you'd see an error message in this case, but what if you miss it? This can be disastrous. For example, if you are accustomed to using rm (remove file(s)) as an alias for rm -i (remove file(s), but prompt for confirmation), when you run rm you will expect a confirmation prompt. If the alias didn't get defined you won't get a prompt, and you may end up removing files you need. That is why we suggest rmi as an alias for this command.

C Shell Family

The format of the alias command is:

% alias [new [old]] 

When you enter new the shell substitutes old.

The first example causes ls -l to be executed when the command ll is entered:

% alias ll ls -l 

The next example creates the command dir to list directory files only:

% alias dir 'ls -l | grep ^d' 

grep in this case searches for a d in the first column of each line.

Bourne Shell Family

Alias

The alias command is supported by ksh and bash, but not sh. For the entire Bourne shell family you can use shell functions instead of aliases; we discuss these below. The format of the alias command is:

% alias name = 'alias_contents' 

The first example causes ls -l to be executed when the command ll is entered:

% alias ll='ls -l' 

The next example creates the command dir to list directory files only:

% alias dir='ls -l | grep ^d' 

grep in this case searches for a d in the first column of each line.

Shell Functions

The Bourne and Korn shells support shell functions, which are similar to shell scripts in that they store a series of commands for execution at a later time. Shell functions are more quickly accessed than scripts because they are stored in memory instead of a file, and the shell preprocesses them. They can be used in place of aliases in order to be completely portable between sh, ksh, and bash. For information on the format of shell functions and how to use them, refer to the sh man pages or a UNIX text.