Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
$VAR vs ${VAR} and to quote or not to quote

I assigned a variable var a value as follows:

pradeep@pradeep-laptop:~$ var="Pradeep
> is     
> a good
> boy"

Then I issued a echo command as:

pradeep@pradeep-laptop:~$ echo $var
Pradeep is a good boy

and for the command:

pradeep@pradeep-laptop:~$ echo "$var"
Pradeep
is 
a good
boy

I want to know why there is difference in the output?

share|improve this question
add comment

marked as duplicate by Shawn J. Goff, Stéphane Gimenez, jasonwryan, Gilles, Kevin Apr 2 '12 at 23:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 3 down vote accepted

Expansions that occur within double quotes (") do not undergo field splitting.


In echo $var, since the expansion of $var does not occur within double-quotes, so it does undergo splitting. The shell runs echo with the 5 arguments: Pradeep, is, a, good, boy. echo prints all of its arguments separated with a space.

In echo "$var", the expansion of $var occurs within double-quotes, so it does not undergo splitting. The shell runs echo with a single argument,

Pradeep
is
a good
boy

which echo faithfully prints.

share|improve this answer
add comment

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