Subject: Re: (s)hell quoting (s)hell
To: Schwerzmann, Stephan <stephan.schwerzmann@schmid-telecom.ch>
From: matthew sporleder <msporleder@gmail.com>
List: netbsd-users
Date: 03/01/2006 10:07:39
For more questions of this nature, buy the follow book:
http://www.oreilly.com/catalog/upt3/index.html

I'm not sure how bash works specifically, but here's netbsd's sh in action:

gomez$ cat test.sh
#!/bin/sh
var=3D\"\"
echo ${var}
gomez$ ./test.sh
""



On 3/1/06, Schwerzmann, Stephan <stephan.schwerzmann@schmid-telecom.ch> wro=
te:
> hello,
>
>   I'm scripting things with bash an have a quoting problem.
>
>   Hopefully I'm not too OT: please ignore or redirect me as
> appropriate...
>
>  - - -
>
>   at different spots in my (ba)shell script a tool (1) is used with
> mostly
> the same set of the options.
>
> (1 insert your favorite here)
>
> """
> :
> tool --optA --optB --optX | ...
> :
> :
> tool --optA --optB --optY | ...
> :
> """
>
>   this calls for factoring out
>
> """
> :
> TOOL=3D"tool --optA --optB "
> :
> :
> ${TOOL} --optX | ...
> :
> :
> ${TOOL} --optY | ...
> :
> """
>
>   troubles start with specific option  --optP  aimed at 'neutralizing'
> an environment variable; that is make tool work as if the env.var would
> not be set but without messing with the env.var itself.
>
>   reading from the doco, it tells to use  --optP ""  (2x doublequotes)
>
>   interactive invocations of   tool --optP ""   are successful
>
>   to bring doublequotes into the factored out string, I change the
> assignment to use single quotes (no evaluations needed in there)
>
> """
> :
> TOOL=3D'tool --optA --optB --optP "" '
> :
> :
> ${TOOL} --optX | ...
> :
> """
>
>   PROBLEM:  sure enough bash, when evaluating var TOOL for the call
> of the tool, surrounds the 2x dblquotes with a pair of single quotes,
> which makes tool fail: the effective command ends up being:
> (traced with set -x)
>
> """
> :
> tool --optA --optB --optP '""' --optX | ...
> :
> """
>
>   of course this also fails in interactive invocation
>
>
>   QUESTIONS:
>  - how can I define a shell var containing a string with
> dbl.quotes in it, representing a command?
>  - how do I have to use such a variable to avoid that the shell
> messes up things?
>
>
> thanks for your time
> Stephan
>