Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 Jul 2003 21:27:05 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Chuck Swiger <cswiger@mac.com>
Cc:        Rob Lahaye <lahaye@users.sourceforge.net>
Subject:   Re: tcsh script: quote and spaces problems
Message-ID:  <20030801022705.GC13080@dan.emsphone.com>
In-Reply-To: <3F29CD39.9080505@mac.com>
References:  <3F29C589.4030009@users.sourceforge.net> <3F29CD39.9080505@mac.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030801022705.GC13080>