[clue-tech] Maddening bash "protection" problem

Jed S. Baer cluemail at jbaer.cotse.net
Tue Feb 10 19:48:06 MST 2009


Hi Folks.

Having an issue with bash. I have a very long command line which I need
to use repeatedly in a script, with just the last 2 params changing. So I
thought hey, I'll just dump the whole thing into a shell variable, and
then I can just re-use that. Kinda like this:

#!/bin/bash

longcmd="dothing -x -e 'param with spaces' more junk"
$longcmd this that
$longcmd other thing

Where the argument inside the single quotes is a parameter which must be
preserved as quoted and passed to the command with the embedded spaces
intact.

However, bash seems intent upon protecting me from myself here, and
insists upon inserting additional quote marks, escaping them, and thus
completely gumming up the works. I've tried many variations, including
escaping the quote marks, doubling the quote marks (this is the method in
some languages for protecting quote marks, but apparently not in bash),
enclosing the entire string in single quotes, and then escaping or
doubling the quote marks, and even using \047 to put the quote marks in.
The results I get vary depending upon which of these I try, and I find
some of them truly bizarre, with multiple escaped quote marks being
inserted.

I've tried searching for various things, like word protection, quote
protection, parameter expansion, and finding every occurrence in the bash
man page of "disable" and "eval" to see if there's a shell option to turn
off whatever this behavior is called. No luck.

So, what say you folks?

Yeah, I know I can just come up with some other way to accomplish the
same thing, and I have, in fact come up with a few ideas, such as using a
shell function, or re-writing in Perl. But having encountered this, I'm
genuinely curious about what bash thinks it needs to be doing here,
besides returning the value assigned to a variable.

A short shell script should demonstrate this on your very own home
computer, should you be curious.

#!/bin/bash

veeble="a 'param with spaces' to keep intact"
echo $veeble
set -x
ls $veeble
set +x
exit

Run that, and then contrast with the error you get when entering the
following at the bash prompt:

  ls 'param with spaces'

(Of course, if you actually have a file in the current directory named
"param with spaces", you'll get different results.)

TIA.
jed


More information about the clue-tech mailing list