tcsh script: quote and spaces problems

Dan Nelson dnelson at allantgroup.com
Thu Jul 31 19:27:06 PDT 2003


In the last episode (Jul 31), Chuck Swiger said:
> Rob Lahaye wrote:
> [ ... ]
> >Any solutions for this problem with quotes and spaces in tcsh
> >script? Or is tcsh not suitable for this kind of things?
> 
> Ugh, the latter.  :-)  /bin/sh handles nested quoting right, but crunches 
> the space together:
> 
> % foo="-f \"t  \""
> % echo $foo
> -f "t "
> 
> % foo='-f "t  "'
> % echo $foo
> -f "t "

Actually it doesn't.  You get this result because sh splits variables
on $IFS before passing the result to a command, so what echo gets is
 argv[1]="-f \"t"
 argv[2]="\""
, and echo always prints its arguments separated by a space.  You can
verify that the variable is set correctly by running "set | grep -a
foo".  To pass the entire string as one argument, run echo "$foo".

> ...however, you might be able to muck with $IFS and get better results. 
> Also, ZSH seems to do exactly what you expected:
> 
> 64-sec% foo="-f \"t  \""
> 65-sec% echo $foo
> -f "t  "

This is because zsh passes variables directly to commands, unless the
SH_WORD_SPLIT flag is set.  You can force spltting with the ${=foo}
syntax.

-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-questions mailing list