I'm trying to comprehend and reproduce this

I read this section in the book Learning The Bash Shell;
My question about it is below it:

"Notice that we used double quotes around variables (and strings containing them) in these echo examples. In Chapter 1, we said that some special characters inside double quotes are still interpreted, while none are interpreted inside single quotes.

A special character that "survives" double quotes is the dollar sign—meaning that variables are evaluated. It's possible to do without the double quotes in some cases; for example, we could have written the above echo command this way:

$ echo The value of \$ varname is \"$ varname \".

But double quotes are more generally correct. Here's why. Suppose we did this:

$ fred='Four spaces between these words.'

Then if we entered the command # echo $fred, the result would be:

Four spaces between these words.

What happened to the extra spaces? Without the double quotes, the shell splits the string into words after substituting the variable's value, as it normally does when it processes command lines. The double quotes circumvent this part of the process (by making the shell think that the whole quoted string is a single word).

Therefore the command echo "$fred" prints this:

Four spaces between these    words.

The distinction between single and double quotes becomes particularly important when we start dealing with variables that contain user or file input later on."

My question

I have tried to practice with this, but I am confused. Do I put-

$ echo The value of \$ varname is \"$ varname \".
$ fred='Four spaces between these words.'

in a file? Like enter # nano, then put it in there? Or do I just enter it into the terminal? Neither are working. When I entered # nano fred and put it in that file and made it executable and changed "varname" to "fred" and ran ./fred, I got this as a result:

The value of $fred is ""

Can you explain what am I doing wrong?

  • Ahhh thats why i'm confused - could you please wrap the bit from the book in a block-quote so it is visible from your question.
    – Wilf
    Jun 26, 2014 at 17:04
  • Okay, I did it.
    – malohhree
    Jun 26, 2014 at 17:06
  • @malohhree please shorten your question. Seems unclear to me. Jun 26, 2014 at 17:10
  • I can't. You need to read the section of the book to understand my question, which is at the bottom.
    – malohhree
    Jun 26, 2014 at 17:19

2 Answers 2


I've tried to break this down a little bit for you, using your own examples/text as well, check it out:

$ cat fred    

fred='Four spaces between these    words.'

echo "The value of \$fred is $fred"

echo "The next line being printed is simply the results of # echo \$fred"

echo $fred

Running the bash script above prints the following to the terminal/stdout:

The value of $fred is Four spaces between these    words.
The next line being printed is simply the results of # echo $fred
Four spaces between these words.

As well, you can view a terminal video of this here: http://showterm.io/98f56243f60551b338b9f


I think you should put:

fred='Four spaces between these words'
echo "The value of \$fred is \"$fred\""

You can enter this into terminal, by right-clicking and selecting Paste, or Ctrl+Shift+V), or by entering it one line at a time. e.g:
                enter image description here

You can also create a script with this in - run nano ./echofredscript and paste in:


#The bit above is a shebang line (look it up on wikipedia).
#These are comments and will be ignored

fred='Four spaces between these words'
echo "The value of \$fred is \"$fred\""

You can then save it with Ctrl+O and exit with Ctrl+X, then run this to make it executable:

chmod +x ./echofredscript

Then this to run the script:


This should result in: enter image description here


  1. No spaces between the $ and the variable name (e.g. $varname not $ varname)
  2. The \ character tells the shell to treat the following character as a normal character. In this case you need to escape the quotes around the variable so they appear in the output, and the first variable so it appears as $fred not Four spaces between these words.

This might also work so you don't have to escape the quotes, but you would still need to escape the variable:

echo "The value of \$fred is '$fred'".
  • But where do I put this? In nano? Or just in the terminal?
    – malohhree
    Jun 26, 2014 at 17:32
  • 1
    nano is just an editor like gedit. Jun 26, 2014 at 17:38
  • you can make a script in nano and run it - it is quicker in terminal. Try and work it out yourself about which you need to use, you can learn then (thats how I did it... I didn't even use a book (till recently))
    – Wilf
    Jun 26, 2014 at 17:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .