Help! Csh is eating my brain....

Steven List itkin at luke.UUCP
Mon Oct 21 04:04:37 AEST 1985


In article <366 at zaphod.UUCP> you write:
>		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.

Aliases use current line history replacement.  \!:n means "replace
with word n of the current command line", where 0 is the first word (usually
the command itself).  If readandset were to be written as a Bourne shell
script, it would be as follows:

	prompt=$1
	echo "$1\c" < /dev/tty > /dev/tty
	read X
	echo X

and used as
	
	chvar=`readandset "Choice? "`

thus storing the user's input into variable chvar.  The use of "$<" in
the alias is the equivalent of "read chvar" in Bourne shell or the
alternate "set chvar = `line`" in C shell (ours does not support $<).

Thus, readandset can be define is pseudocode as

	print the second word of this command as a prompt without a
		following newline
	store the response typed in in the variable named as the second
		argument (word 2, third object)

The use of \!:n, where n can be any number, * (for all), or $ (for last),
is a very powerful means of defining macros in the C shell.  I use them
all over the place very effectively (also refer to various means of 
implementing popd, pushd directory change macros).
-- 
***
*  Steven List @ Benetics Corporation, Mt. View, CA
*  Just part of the stock at "Uncle Bene's Farm"
*  {cdp,greipa,idi,oliveb,plx,sun,tolerant}!bene!luke!itkin
***



More information about the Comp.unix mailing list