Double Quotes

Clearly we can use ticks and quotes interchangeably unless we need to honour special characters in the shell. So let's start again, this time using quotes instead of ticks. I'm going to set the NAME variable again:

NAME="hamish"
echo "$NAME"
            

As expected:

hamish
            

Thus, the main difference between ticks and quotes is that quotes honour special characters. How do we produce the following output with echo and our NAME variable?

Hello. The name in "$NAME" is hamish. 
            

We have got a variable NAME that currently holds the value 'hamish'.

If you're using double quotes and you need to use double quotes within a double quoted string, you need to escape the double quotes. You want to print out a '$NAME' and since the $ is a special character, you need to escape the dollar itself. So, the answer is:

echo "Hello. The name in \"\$NAME\" is $NAME". 
            

That looks quite complex but it's relatively straightforward. The escape character ( \ ), escapes special characters within this quote. We need double quotes, how do we do that? We escape the double quotes ( \" ). We need a dollar sign, how do we do that? We escape the dollar ( \$ ). Try this now.

So quotes honour things like the backslash, the dollar and the backtick.

If we wanted to, we could append to our previous example:

echo "Hello. The name in \"\$NAME\" is $NAME. Today"s date is: `date`"
            

We would get output similar to the following:

Hello. The name in "$NAME" is hamish. Today"s date is: Sun Nov 30 22:32:38 SAST 2003
            

Now, you'll see that the quotes have honoured the backslash, the dollar and the backtick by executing the date command itself.

So, double quotes are probably the safest thing that you're going to want to use in your script, because they generally honour most of the things that you're expecting them to honour like variable names.

In order to achieve a double quote in the above string, I escaped it with a backslash. In order to achieve a dollar, I escaped it with a backslash.

So any character you need to put in the string, that's a special character, you need to escape. What about putting a backslash in a string? How do you achieve that? For example how would you produce the following string with echo:

the path is \\windoze\myshare
            

Remember, backslash is a special character, it's an escape character. Try:

echo "the path is \\windoze\myshare"
            

You'll end up with:

the path is \windoze\myshare
            

Well, let's try something different. If you wanted to achieve double backslashes, you need to escape the backslash. Instead of having a double backslash, you now need triple backslashes:

echo "the path is \\\windoze\myshare"
            

This is because the first backslash escapes the second backslash, which gives you a backslash, and the backslash that you already have. So you end up with two backslashes.[17]

Some other useful backslash commands are:

command action
\n newline
\t tab
\b bell

If you do a man or info on echo, you will see what these special characters are.

Exercises

What do the following commands do. Explain why.

  1. echo "Hello $USERNAME"

  2. echo "Hello $USERNAME. \'I am king of this candy pile\'. And "You""

  3. echo 'Hello. My $USERNAME is "$USERNAME". This quoting stuff can get a bit tricky'

  4. echo "Hello. My '$USERNAME' is $USERNAME. This quoting stuff can get a bit tricky"

  5. echo "Hello. My \$USERNAME is $USERNAME. This quoting stuff can get a bit tricky"

  6. echo -e "This is what happens with special characters ( bell for example ) \b\b\b\b". What does the -e do?

  7. echo -e "Name\tSurname\tAge\nHamish\tWhittal\t36\nRiaan\tB\t29\n"

  8. echo "\\$USERNAME\\\home"

  9. echo "\$USERNAME\\\home"



[17] When doing the Network Administration course for example, and you may need to map a shared disk. In Windoze we would:

	net use Z: \\Windoze\share
                    

In Linux, you would need to do:

smbmount /tmp/mymount \\\Windows\\share