Single Quotes or "ticks"

What is the purpose of ticks? Ticks don't recognise certain characters in the shell. So if we had set a variable:

NAME="hamish"

echo '$NAME'
            

will produce:

$NAME
            

not the value stored in the variable:

hamish
            

Why? Ticks don't honour special characters such as the dollar sign.

Another example would be that we know "<" means redirect, so if we said:

echo '$NAME<file'
            

We would get back:

$NAME<file
            

Ticks do not honour special characters in the shell. For example, if we want to run a process in the background by putting an ampersand ( & ) on the end:

echo '$NAME<file &amp;'
            

All we're going to get back is:

$NAME<file &amp;
            

You should use quotation marks if you're setting variables.

This time I'm going to set a variable called FULLNAME:

FULLNAME=Hamish Whittal
            

Now if you try the above command, you'll find that it doesn't do quite as expected. It'll produce:

bash: Whittal: command not found
            

And furthermore, if you:

echo :$FULLNAME:
            

You will see that it has set FULLNAME to a NULL value:

::
            

How do we use spaces inside strings? We can tick them. Let's try the same command but enclose our string in ticks:

FULLNAME='Hamish Whittal'
echo $FULLNAME
            

This will now produce the full name:

Hamish Whittal
            

What's interesting about single ticks, is because they don't honour special characters and space is seen as a special character, we could say:

FULLNAME='Hamish       Whittal'
echo $FULLNAME
            

You'll find that it still produces the same output:

Hamish Whittal
            

In the same way, if you wanted to grep for the pattern 'Linus Torvals' from the file bazaar.txt you have to enclose the pattern in ticks otherwise it would be looking for 'Linus' in two files: Torvals and bazaar.txt.[16] Thus:

grep 'Linus Torvals' bazaar.txt
            

Ticks can be used in a number of ways. They can be used to not interpret special characters, they can be used to set environment variables, they can be used in regular expressions and they also honour a new line. If we said:

echo 'Hello 
< World' 
            

This will produce the following

Hello
World
            

Exercises:

What do the following commands do. Explain why.

  1. echo 'Hello $USERNAME. How are you today?'

  2. touch '{hello,bye}.{world,earth}' vs. touch {hello,bye}.{world,earth}

  3. echo 'echo $USERNAME'

  4. echo 'Hello 'Joe'. How are you?'

  5. echo 'Hello \'Joe\'. How are you?'



[16] Not including ticks would mean that grep would see two file names after the pattern Linus and would then be looking for the word Linus in the file Torvals and bazaar.txt. There is no file called Torvals on the machine.