Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ 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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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