What is the correct way of quoting named arguments in bash?

In bash I can use named arguments like this:

command --argument=value

What is the correct way of quoting them if I need to include spaces?

command --argument="long value"
or
command "--argument=long value"

>Solution :

What is the correct way of quoting them if I need to include spaces?

Any quoting that does the job – preserves the space that you want to preserve – is fine. Any of these run the same command:

echo cmd --argument="long value"
echo cmd "--argument=long value"
echo cmd --argument=long\ value
echo cmd --argument=long' 'value
echo cmd --argument=long" "value
echo cmd --argument=long" value"
echo cmd --argument=l'ong 'value
echo cmd '--argument=long value'
echo cmd '''--argument=long value'''   # same as above, with empty '' pairs
echo cmd '-'-'a'r'g'u'm'e'n't'='l'o'n'g v'a'l'u'e'  # just having fun

You can check if your quoting is fine with set -x and inspecting the output.

$ set -x
$ echo cmd --argument="long value"
+ echo cmd '--argument=long value'
$ echo cmd "--argument=long value"
+ echo cmd '--argument=long value'

Leave a Reply