Help! Csh is eating my brain....

Karl Kleinpaste karl at osu-eddie.UUCP
Fri Oct 18 23:35:18 AEST 1985


> 	I'm trying to debug a csh script...
> 		...
> 		alias readandset 'echo -n \!:1 ; set \!:2 = $< ' 
> 		...
> 		readandset "Choice? " chvar
> 		...
> 		if("$chvar" == "quit") ....
> 	In particular I'd like to know what \!:1 or \!:2 means/does.

In csh, the alias command sets the aliased name to the value of the
other words in the line.  In this example, that's one long quoted
word, but that's OK.

Within the alias command, one can use standard csh history expansions
to parse the arguments to be given to the alias itself.  That is, the
alias, once expanded, is considered momentarily to be the previous com-
mand, which makes it available for history expansion.

In history references, the ! sequences introduce pieces of that event.
Specifically, anything of the form <history reference>:<number> refers
to the <number>th argument of the command specified by the <history
reference>.  The usage of ! by itself refers to the immediately
preceding event, i.e., the aliased command itself.  Thus, in the call
	readandset "Choice?" chvar
the string "Choice?" is the 1st argument, and chvar is the 2nd.  Hence,
when the entire command using the readandset alias is used, it creates
the following command line:
	echo -n "Choice?" ; set chvar = $<
which lets the user input a choice as variable chvar.

The reason that the ! was escaped as \! in the alias command itself was
to keep the alias command itself from referring to history events; it
introduced the literal character ! into the alias declaration, so that
it would be available during alias expansion to parse arguments.
-- 
Karl Kleinpaste



More information about the Comp.unix.wizards mailing list