Quotations

Shell has single and double quotations to signify a string. However, they do have great differences between them.

Single Quotation (')

Single quotation instructs shell not to interpret any special characters like backslash and variable and treat it as a pure string.

$ echo ‘hello’’world’
Helloworld
$ echo ‘hello world’
hello world
$ echo ‘$hello’$world
$hello
$ echo ‘hello
> world’
hello
world
$ echo *
<all sort of files>
$ echo ‘*’
*
$ world='universe'
$ echo 'Hello $world'
Hello $world
$

This is suitable for solid string statement where you want to form a string or a command without interpretation.


Exception

If you wish to have special characters like newline (\n) interpreted, add a dollar sign ($) in front. E.g.:

$ echo $'Name\tAge\nBob\t24\nMary\t36'
Name  Age
Bob   24
Mary  36
$

You got a table instead of the long weird sentence.

Double Quotation (")

Double quotation instructs shell to interpret any special character within the line, generating the resultant output as a string.

$ echo “hello””world”
helloworld
$ echo “hello world”
hello world
$ echo “$hello”$world
$ echo “hello
> world”
hello
world
$ echo *
...
$ echo “*”
*
$ 
$ world="universe"
$ echo "Hello $world"
Hello universe
$


Backslash (\n)

Backslash uses for 2 functionalities:

  1. Denote special characters like newline (\n), tab (\t), etc.
  2. Cancel functional character and retain it as a string, like double-quote itself.

They are interpreted by double quotation automatically. Example:

$ world="universe"
$ echo -e "Hello \n $world"
Hello 
 universe
$ echo "Hello \"Hermione\""
Hello "Hermione"
$

That's all about quotation in SHELL. Use it to your advantage! You may proceed to the next section.