Wildcards and special characters in an alias
Arguments to aliases
Implementation details
See also
Aliases are a quick shorthand technique. If you type an alias at the beginning of a command, it’s replaced by whatever the alias is defined as. They’re intended to be used for relatively simple abbreviations: if any arguments are needed, you have to be able to type them onto the end. In more complex situations, procedures are usually a better choice.
To list the aliases currently defined, use the alias command. Some aliases are predefined for you.
Some aliases are used to intercept references to cmd.exe’s builtin commands. For example, this is how dir is run. Other aliases give simple alternate names to a command, e.g., rename for mv. Still others are used to customize a command with a useful option. For example, mi runs more but starts it immediately in interactive mode, which means the screen is cleared first; in a console window, this tends to run faster.
To find out how any particular alias is defined, use the alias command with only the name you’re interested in as an operand.
To create a new alias, type the alias command followed by the name of alias being created and word list it should be expanded into:
If you define an alias that refers to itself, either directly or via other aliases, the shell traps the reference rather than allowing it to expand without limit:
This raises the question how you might define an alias, say, ls, that intercepts references to the ls utility without getting into a loop. The answer is that the shell considers it a special case if the first word in the expansion of the alias is the same as its name. Here’s an alias that causes ls to always display all files:
Other ways around the problem would be to use the full ls.exe name inside the alias or put quotes around the ls.
Recall that any wildcards or escape characters in a command are always processed before the command itself is run. Suppose you wanted to define an alias to list all your .exe files in the current directory. Watch what happens if we’re not careful:
What happened? Why didn’t lexe give us the files in our new current directory? The answer is clear if we list the alias definition:
The *.exe wildcard got expanded before the alias definition was processed. If we want to defer processing of the wildcard until later, when the alias is used, we need to escape the * character:
It’s just a little trickier if we want to turn off the special meaning not only when defining the alias but also when we use it. That’s done by embedding an extra ^^ sequence into what we type, knowing that’ll turn into a single escape character in the definition. Here’s an example:
Normally, any words on the line following the reference to the alias will simply be tacked onto the expansion. For example:
But suppose you’d like to paste the arguments into the middle of the expansion? The way to do that is with a very special adaptation of the history mechanism. If the expansion contains any history references, the C shell treats this as a special case. It temporarily adds the original text of that command to the history list. Note that only the command itself – starting with the alias name and ending just before any semicolon, closing parenthesis or pipe symbol terminating the command – not the whole line is added. Any history references in the definition of the alias are processed and the result taken as the final command text. The temporary history entry is then discarded along with the original string of argument words. Here’s an example:
Notice the use of the escape characters to embed the history reference, !^ and the literal ? into the alias definition.
It is not necessary for the alias to use all the argument words. Any that it doesn’t reference using the history mechanism are just discarded. Notice that this alias grabbed only the first argument word from history. Suppose there were other words on the line:
See how the rest were just discarded?
The alias mechanism is actually part of the parsing mechanism rather than a run-time feature of the C shell. What that means is that the alias expansion is done when the statement is first read, not when it’s executed. Here’s an example where we attempt to change the definition of an alias inside a loop. Notice that it doesn’t have any effect until we exit the loop. That’s because the whole loop is being compiled as a block before any part of it is executed.
Also, when an alias substitution is done, the expansion is taken as the series of words the alias was defined as; it’s not reparsed into words all over again. Here's an example using the myecho sample application, which simply dumps the argv command line argument words it's given with quotes around each.
Notice that you can’t really see that the test alias is defined as just two words, myecho and this is a test when you list it (it’s just like listing a variable set to a list of words some of which contain spaces), but that’s what it is. These word boundaries are maintained even if the alias contains history references.
Aliases
Predefined aliases
Procedures
Builtin procedures
History recall
Quoting
Variable substitution
Wildcarding
Order of evaluation
Tutorial: Procedures
Tutorial: History
Tutorial: Quoting
Tutorial: Variables
Tutorial: Wildcarding