Quotes

Author

Marie-Hélène Burle

Let’s experiment with quotes:

variable=This string is the value of the variable
echo $variable
bash: line 1: string: command not found

Oops…

variable="This string is the value of the variable"
echo $variable
This string is the value of the variable
variable='This string is the value of the variable'
echo $variable
This string is the value of the variable
variable='This string's the value of the variable'
echo $variable
bash: -c: line 1: unexpected EOF while looking for matching `''

Oops…

One solution to this is to use double quotes:

variable="This string's the value of the variable"
echo $variable
This string's the value of the variable

Alternatively, single quotes can be escaped:

variable='This string'"'"'s the value of the variable'
echo $variable
This string's the value of the variable

Admittedly, this last one is a little crazy. It is the way to escape single quotes in single-quoted strings.

The first ' ends the first string, both " create a double-quoted string with ' (escaped) in it, then the last ' starts the second string.

Escaping double quotes is a lot easier and simply requires \".