$ ourSite="super    user"
$ echo $outSite
super user
$ echo "$outSite"
super    user

bash automatically compress adjacent SPACEs into one if we do not use double quotes to quote the variable.

I wander why bash want to do like this? That variable is just a string of characters, isn't is better to substitute the variable to what it originally is by default?


There isn't any compression going on here. Your example:

 echo $outSite

is equivalent to:

echo super    user

The echo command, like all shell commands, does not see the whole command line. Instead, it sees an array of arguments that are parsed for it by the shell. Like it says in bash(1), a command line argument is separated from the next argument by 1 or more spaces. So when the shell is parsing the command line, it pulls the arguments out, one by one, and passes them to echo. (We'll skip over the fact that echo is not a separate program, since conceptually it acts like one.) So echo sees a first argument "super" and a second argument "user" and doesn't even know how many speaces separate the two arguments. It prints them, one by one, with a single space separator. The other command:

echo "$outSite"

exactly equivalent to

echo "super    user"

passes one argument, and that argument has embedded spaces, which echo obediently prints out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.