Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

Aliases

Oregon Coast

Aliases
Previous | Next

Topics

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.

335 D% alias cdd cd +c copy local s ; @ s = nowild ; @ nowild = 1 ; safecopy date dt del local s ; @ s = nowild ; @ nowild = 1 ; safedel dir cmd /c dir erase del h history label hlabel ll ls -L md mkdir mi more -i more hmore rd rmdir ren rename rename local s ; @ s = nowild ; @ nowild = 1 ; saferename ro rotd start env -N type cat vol vl xcopy local s ; @ s = nowild ; @ nowild = 1 ; safexcopy

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.

336 D% alias mi mi more -i

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:

337 D% alias hello echo hello world 338 D% hello hello world

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:

339 D% alias hello (echo infinite; hello again) 340 D% hello csh: A loop in the alias definitions was encountered and trapped.

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:

341 D% alias ls ls +a

Other ways around the problem would be to use the full ls.exe name inside the alias or put quotes around the ls.

Wildcards and special characters in an alias

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:

342 D% cd d:\Program Files\Hamilton C shell 2012 x64\Samples 343 D% alias lexe ls *.exe 344 D% lexe args.exe dumpenv.exe rcode.exe blksize.exe myecho.exe winerror.exe 345 D% cd ..\bin 346 D% lexe ls: 'args.exe' does not exist. ls: 'blksize.exe' does not exist. ls: 'dumpenv.exe' does not exist. ls: 'myecho.exe' does not exist. ls: 'rcode.exe' does not exist. ls: 'winerror.exe' does not exist.

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:

347 D% alias lexe lexe ls args.exe blksize.exe dumpenv.exe myecho.exe rcode.exe winerror.exe

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:

348 D% alias lexe ls ^*.exe 349 D% alias lexe lexe ls *.exe

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:

350 D% alias hello echo How are you^^^? 351 D% alias hello hello How are you^? 352 D% hello How are you?

Arguments to aliases

Normally, any words on the line following the reference to the alias will simply be tacked onto the expansion. For example:

353 D% alias hello echo hello world 354 D% hello how are you hello world how are you

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:

355 D% alias hello echo hi ^!^^, how are you^^^? 356 D% alias hello hello echo hi !^ how are you^? 357 D% echo x; hello Frank; echo y x hi Frank, how are you? y

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:

358 D% hi Frank and Bob hi Frank, how are you?

See how the rest were just discarded?

Implementation details

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.

359 D% alias foo echo this is life 360 D% foreach i (hello world) 361 D? alias foo echo $i 362 D? foo 363 D? end this is life this is life 364 D% foo world

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.

365 D% alias test myecho "this is a test" 366 D% alias test test myecho this is a test 367 D% test 'myecho' 'this is a test' arg length = 22 characters

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.

See also

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

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.