Twitter

Some bash tips -- 7 -- single quotes vs double quotes

This blog is part of a bash tips list I find useful to use on every script -- the whole list can be found here.

Quoting your variables is very important in shell as we previously saw here. But this previous blog was all about double quoting the variables. So what's the difference between single quotes and double quotes?

Documentation says that double quotes allows variable expansion whether single quotes don't. As you may not be familiar with this vocab, let's illustrate this with an example:
$ A="I am the value of variable A"
$ echo "${A}"   <== double quotes, variable expansion
I am the value of variable A
$ echo '${A}'   <== single quotes, no variable expansion
${A}
$
This means that double quoting a variable will show you the value of the variable, single quoting a variable will show you the name of the variable. Easy.

Now, let's save this in a file:
$ echo "${A}" > afile
$ cat afile
I am the value of variable A
$ echo '${A}' >> afile
$ cat afile
I am the value of variable A
${A}
$
We now have a file with 2 lines, one line with the value of the A variable and another line with the name of the variable. We will show below that the different quoting mechanism also works same with grep:
$ cat afile | grep "${A}"
I am the value of variable A
$ cat afile | grep '${A}'
${A}
$
And it is also works the same way with sed, let's introduce a variable B and replace A with B to showcase this, double quoting will replace the value of A with the value of B:
$ B="this is B variable value !"
$ cat afile | sed s"/${A}/${B}/"
this is B variable value !   <=== the value of A has been replaced by the value of B
${A}
$
If we single quote the same sed command the name of the variable A is replaced by the name of the variable B, not the value / content of the variables:
$ cat afile | sed s'/${A}/${B}/'
I am the value of variable A
${B}  <== the name ${A} has been replaced by ${B}
$
And now, how to replace the value of the variable A not by the value of the variable B but by its name ? well there is another thing in variable expansion, variable expansion happens between double quotes and when something starts with a $; we then just have to \ the variable to escape it from it special meaning:
$ cat afile | sed s"/${A}/\${B}/"
${B}   <== the value of variable A is replaced by "${B}", not by its content as the $ is no more special after a \
${A}
$

Hope this shed some lights on this very important Unix mechanism; experiment and enjoy !

< Previous bash tip / Next bash tip >

2 comments:

  1. Hi Sir,
    Within a single statement, can we use both single quotes and double quotes? If so, could you illustrate with one example?
    Thank you

    ReplyDelete
    Replies
    1. Not sure to understand what you mean ? what would you like to do ?

      Delete

CUDA: Getting started on Google Colab

While getting started with CUDA on Windows or on WSL (same on Linux) requires to install some stuff, it is not the case when using Google...