Re: Yet another "stop the bash quoting" post

From: Paul Colquhoun (postmaster_at_andor.dropbear.id.au)
Date: 11/29/04


Date: Mon, 29 Nov 2004 03:00:41 GMT

On Sun, 28 Nov 2004 19:48:22 GMT, Allen Kistler <ackistler@oohay.moc> wrote:
| rakesh sharma wrote:
|>
|> Try this:
|>
|> ##### file1.sh ####
|> #!/bin/bash -u
|> CMD="./zxc.sh \"-o'a b'\""
|> echo "\$CMD=|$CMD|"
|> eval "
|> set -x
|> $CMD
|> set +x
|> "
|> ######### end of file1.sh ##########
|>
|> ####### zxc.sh ########
|> echo "Inside the file... '$0'"
|> for arg
|> do
|> echo "arg=|$arg|"
|> done
|> ############ end of zxc.sh ###########
|>
|> Output from running the file1.sh command is:
|>
|> $CMD=|./zxc.sh "-o'a b'"|
|> + ./zxc.sh -o'a b'
|> Inside the file... './zxc.sh'
|> arg=|-o'a b'|
|> + set +x
|>
|>
|>
|> Is this what you wanted? But remember that here the argumetnt
|> contains the a real single quote.
|>
|> Even from the command line when you say:
|> command -o'a b'
|>
|> the argument doesn't contain those single quotes; they"re there to protect
|> the spaces between a and b & prevent wildcard expansions.
|> So I still feel that my previous reply is what you really need.
|>
|>
|> What you see is not what the command sees because the entity shell is
|> performing some operations silently/behind the scenes.
|>
|> command -o'A B'
|>
|> will entail 'command' being passed only 1 argument
|> "-oA B" (double quotes for emphasis not part of the argument)
|>
|> In the case I showed today, .... ok it prints -o'a b' but the argument
|> that 'command' gets from the shell contains those single quotes too.
|>
|> Unless you intend to do 2/more levels of evaling it's an error.
|
| By using set -x in the bash script and running /command/ without the
| eval, I can see that bash sees this as
| '"-oa b"'
| Assigning it to CMD and using eval, I get
| '-o'\''a b'\'''
|
| Unfortunately the binary (sendmail BTW, just to make it real) is still
| not recognizing either as a switch, probably because they start with a
| quote, not a dash. It interprets, e.g., '-oa b' as a recipient, not an
| option. The same problem exists for any binary that takes switches, not
| just sendmail, though.
|
| Maybe the better way to pose the problem is:
| Is there any way to make bash be satisfied that the quotes around a
| space are *not* the outermost characters in a string.
| -o'a b' still has quotes around the space, after all, which is probably
| why it works fine on the command line.

What sendmail option needs a space in it's parameters?

Here are some bash scripts I wrote, and their output. Most quoting
options seem to follow through correctly, without having to get very
convoluted.

$ cat example_command
#!/bin/bash

echo '======================'
[ "$0" ] && echo "\$0 = '$0'"
[ "$1" ] && echo "\$1 = '$1'"
[ "$2" ] && echo "\$2 = '$2'"
[ "$3" ] && echo "\$3 = '$3'"
[ "$4" ] && echo "\$4 = '$4'"
[ "$5" ] && echo "\$5 = '$5'"
[ "$6" ] && echo "\$6 = '$6'"
[ "$7" ] && echo "\$7 = '$7'"
[ "$8" ] && echo "\$8 = '$8'"
[ "$9" ] && echo "\$9 = '$9'"
echo '======================'
echo

$ cat example_wrapper
#!/bin/bash

./example_command -oa b
./example_command '-oa b'
./example_command -o'a b'
./example_command -oa\ b

CMD="./example_command -oa b"; eval $CMD
CMD="./example_command '-oa b'"; eval $CMD
CMD="./example_command -o'a b'"; eval $CMD
CMD="./example_command -oa\ b"; eval $CMD
CMD="./example_command -oa\\\ b"; eval $CMD

$ ./example_wrapper
======================
$0 = './example_command'
$1 = '-oa'
$2 = 'b'
======================

======================
$0 = './example_command'
$1 = '-oa b'
======================

======================
$0 = './example_command'
$1 = '-oa b'
======================

======================
$0 = './example_command'
$1 = '-oa b'
======================

======================
$0 = './example_command'
$1 = '-oa'
$2 = 'b'
======================

======================
$0 = './example_command'
$1 = '-oa b'
======================

======================
$0 = './example_command'
$1 = '-oa b'
======================

======================
$0 = './example_command'
$1 = '-oa b'
======================

======================
$0 = './example_command'
$1 = '-oa\'
$2 = 'b'
======================

-- 
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
     Asking for technical help in newsgroups?  Read this first:
        http://catb.org/~esr/faqs/smart-questions.html#intro


Relevant Pages

  • Re: Yet another "stop the bash quoting" post
    ... |> Output from running the file1.sh command is: ... |> the argument doesn't contain those single quotes; ... | By using set -x in the bash script and running /command/ without the ...
    (comp.unix.shell)
  • Re: Problem in bash script with single and double quotes
    ... interactive bash shell by using the command ... The single and double quotes must be present as shown. ... command that I want to run. ... This line comes from echo is exactly what ...
    (comp.unix.shell)
  • Re: [SLE] bash, single quotes, double quotes, escape characters
    ... > I'm trying to do an ldapsearch in a bash script. ... > If I use double quotes around the filter like this: ... > the command runs but the variable does not get expaned. ...
    (SuSE)
  • Re: shell escape problem?
    ... If that doesn't work, try escaping the quotes: ... bash: echo "Hello": command not found ...
    (Debian-User)
  • security problems in bash script
    ... I have this simple bash script that reads from standard input and executes ... meant to be in the form of a command followed by an argument. ... echo "wrong command" ...
    (comp.programming)