Why bash paste quotes that I didnโ€™t ask for?

Can someone please explain this to me?

$ 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 $ 

In particular, why does the echo line insert ' characters that I didn't ask for, and why does it leave a line that doesn't break?

+6
source share
4 answers

Shell Extension Overview

Bash executes shell extensions in the given order. The -x flag allows you to see the intermediate results of the steps that Bash performs, as it tokens and extends the words that make up the input string.

In other words, the output works as designed. If you are not trying to debug tokenization, word splitting or extension, the intermediate results should not matter to you.

+4
source

(Good question)

"characters don't really exist.

I would describe what you see, as the -x functions try to eliminate how it handles, keeping the line intact. The + sign in front of a separate line with echo in it shows you that this is the debug / trace output of the shell.

Please note that the final result exactly matches your purpose, i.e. X=...

Ihth

+2
source

Your confusion seems to be related to this + echo --vendor 'Bleep\' Bloop . The reason this happens is because it prints what it will look like with the X extension. In other words, executing $X makes it possible to put independent โ€œwords" --vendor , Bleep\ and Bloop on the command line. However, this means that Bloop\ is a word and does not allow \ interpreted to exit (space), it saves \ . If they are intended for parameters for another command, I would suggest doing either:

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

or

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

but I'm 100% not sure if they work. If you want to save the parameters for a command that you could do:

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

And then use them like:

 echo ${ARGS[@]} 
+1
source

This code

 echo --vendor 'Bleep\' Bloop 

will produce the same result as

 echo "--vendor Bleep\ Bloop" 

Bash only reinterprets your code into its own code using the debug / trace option. The reasons for this are probably historical and should not be taken care of.

+1
source

All Articles