DistributedNetworks DistributedNetworks

Unix Concepts   «Prev 

Quoting an alias Definition

You can define aliases that range from the simple to the complex. Alias definitions can include shell features such as file name wildcards or shell variables, and even may contain multiple commands.
When an alias definition contains symbols that have special meaning to the shell, you must enclose the definition in quotes. For example, suppose you want to define an alias that takes you to your home directory and lists the files there. In other words, you would like a shortcut for this command sequence:

cd; ls

The shell treats the semicolon (;) as a command separator. When the cd command finishes, the ls command will run.
Now suppose you want to assign the alias name top to the cd; ls sequence. The following attempt is wrong:
% alias top cd; ls

The shell actually performs two commands, not what you intended. The first command, alias top cd, defines top as an alias to cd only. Next, the shell sees the semicolon and ends the alias command. The shell then sees a second command, ls, and lists your current directory, wherever you happen to be.
The correct definition is this:
% alias top 'cd; ls'
Because the definition is quoted, the shell treats cd; ls as a single argument. In this way, the semicolon is used by the alias definition, not by the shell. In some cases (including this example), it does not matter whether you use single quotes or double quotes. In other cases, it does matter.
The value of quoting and the difference between single and double quotes will be explored in Module 4.