Home > Software design >  Bash Quoting Subshell Command Option With Equal Sign
Bash Quoting Subshell Command Option With Equal Sign

Time:02-02

Why is this

function wtf()
{
  args="$*"
  set -o xtrace
  $(some_prog --option="$args") # note the '='
  set  o xtrace
}
$ wtf asd asd asd asd 
   some_prog '--option=asd asd asd asd'

not the same as

function wtf2()
{
  args="$*"
  set -o xtrace
  $(some_prog --option "$args") # note no '='
  set  o xtrace
}
$ wtf2 asd asd asd asd
   some_prog --option 'asd asd asd asd'

I believe I am running afoul of item #4 of Simple Command Expansion, but I'm not sure...

For reference, I want the behavior of the latter, to have $args be single-quoted. But I want to be able to use the equals sign, and I don't understand why the initial ' quote from $args is being propagated to the whole of --option=.

CodePudding user response:

The output of the -x will quote an entire word should any character in that word need to be escaped. That's why you see

'--option=asd asd asd asd'

instead of

--option='asd asd asd asd'

in the first case.

In the second case, the option and its argument are provided as two distinct words. Since nothing in --option requires quoting, it is not quoted.

The result is the same as far as some_prog is concerned. It will either use the word following --option, or the string following the = in --option=..., as the argument to the option named option.

(In neither case does some_prog actually seen any of the quotes; it only sees the two strings --option and asd asd asd asd or the single string --option=asd asd asd asd. It will split the latter on the = itself when needed.)

  •  Tags:  
  • Related