scoates
scoates

Reputation: 853

Why does bash insert quotes I didn't ask for?

Can someone explain this to me, please?

$ set -x
$ export X="--vendor Bleep\ Bloop"; echo $X
+ export 'X=--vendor Bleep\ Bloop'
+ X='--vendor Bleep\ Bloop'
+ echo --vendor 'Bleep\' Bloop
--vendor Bleep\ Bloop
$

Specifically, why does the echo line insert ' characters that I didn't ask for, and why does it leave the string looking unterminated?

Upvotes: 7

Views: 183

Answers (5)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84433

Understanding Shell Expansions

Bash performs shell expansions in a set order. The -x flag allows you to see the intermediate results of the steps that Bash takes as it tokenizes and expands the words that compose the input line.

In other words, the output is operating as designed. Unless you're trying to debug tokenization, word-splitting, or expansion, the intermediate results shouldn't really matter to you.

Upvotes: 4

Nico
Nico

Reputation: 6913

This code

echo --vendor 'Bleep\' Bloop

produce the exact same output as

echo "--vendor Bleep\ Bloop"

Bash is only reinterpreting your code into it's own code via the debug/trace option. Reasons for this are probably historical and should not be cared about.

Upvotes: 1

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28835

Your confusion seems to be arising more from this + echo --vendor 'Bleep\' Bloop. The reason it appears like that is because it is printing what it would look like when you expand X. In other words doing $X evaluates to putting the independent "words" --vendor, Bleep\, and Bloop on the command line. However, this means that Bloop\ is a word and to prevent the \ from being interpreted to escape the (space), it is preserving the \. If these are meant to be parameters to a different command, I would suggest doing either:

export X='--vendor "Bleep Bloop"'

or

export X="--vendor \"Bleep Bloop\""

but I'm 100% not sure if either work. If you want to store parameters to a command you could do:

# optional:
# declare -a ARGS
ARGS=('--vendor' '"Bleep Bloop"')

And then use them as:

echo ${ARGS[@]}

Upvotes: 1

shellter
shellter

Reputation: 37318

(Good question)

the ' chars aren't really there.

I would describe what you see as the -x features attempt to disambiguate how it is handling keeping your string intact. The + sign at the front of separate line with echo in it shows you that this is shell debug/trace output.

Note that the final output is exactly like your assignment, i.e. X=...

IHTH

Upvotes: 2

Zombo
Zombo

Reputation: 1

When you set -x you are telling Bash to print its interpretation of every command you put in.

So when you put in

export X="--vendor Bleep\ Bloop"

Bash sees it as

export 'X=--vendor Bleep\ Bloop'

and prints as such.

Upvotes: 0

Related Questions