Subject: (s)hell quoting (s)hell
To: None <netbsd-users@netbsd.org>
From: Schwerzmann, Stephan <stephan.schwerzmann@schmid-telecom.ch>
List: netbsd-users
Date: 03/01/2006 12:15:11
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'=20
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=20
of the tool, surrounds the 2x dblquotes with a pair of single quotes,
which makes tool fail: the effective command ends up being:=20
(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=20
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