This is pretty basic knowledge but I’ve helped a few people out recently that had been using Unix/Linux for a while and didn’t know and it sure helped me out when I figured it out. If you had asked me how many quotation marks were on a keyboard before I started doing Bash stuff I would have said two. But I, and it seems most non-programmers, often forget the little `
on the same key as the tilde ~ (to the left of the numbers on standard keyboards). So there are actually three types of quotation marks and each one means something different to Unix:
- Single quote/forward quote
'
- Just for clarity this is the key next to the Enter key on most keyboards. A pair of single quotes tells Unix that the contents are a string and that it should not mess around inside. For example:
x=17;echo 'This is $x';
will returnThis is $x
. Unix did not replace the variable$x
with 17. - Double quote
"
- Double quotes again tell Unix that the contents are a string. Double quotes and the above single quotes useful for keeping Unix from messing up spaces. For example:
grep "To be or not to be" hamlet.txt
will search hamlet.txt for the famous line. In contrast,grep To be or not to be hamlet.txt
searches forTo
in the filesbe, or, not, to, be, hamlet.txt
(not what was intended). Double quotes are different from single quotes in that they allow Unix to replace variables inside them. For example:x=17;echo "This is $x";
will returnThis is 17
. - Backtick/back quote
`
- Just for clarity this is the key shared with ~ on most keyboards. These are really different than the other two. They tell Unix to run whatever command is inside and paste in the output. These are equivalent to using
$()
. For example:echo "You are in `pwd`"
will returnYou are in /usr/bin/
or whatever directory you are in. These can come in really handy. One common use is looping through files. Here’s a slightly more complex example that uses both double and back quotes to add the filename to every row in all text files in a directory:for i in `ls *.txt`;do sed "s/^/$i /" $i >$i.new;done
Knowing how these are used can really help with using Unix. The difference between 'commands'
and "commands"
is pretty subtle but can make a big difference in the results. I learned this the hard way but hopefully now you won’t have to.
Anne | 26-Nov-08 at 11:45 am | Permalink
Thanks for this post. You’re really able to explain the differences between the three quotation marks and I easily understood it.
dipa shah | 13-Jul-12 at 11:41 am | Permalink
tnks this is very useful to me
paan | 20-Jul-12 at 7:39 am | Permalink
you really explain these point very easily.
thanks for sharing such a wonderful contents :)
Gorgi | 01-Oct-12 at 5:21 am | Permalink
Thank you for this, it cleared up the air regarding quotation marks!!!
Kent | 16-Dec-12 at 7:39 am | Permalink
I also wanted to say thank you for your clear explanations. Keep up the good work.
Hanish Wadhawan | 01-Jan-13 at 3:09 pm | Permalink
Thank you for the post. This was really very very clear, easy and helpful. Thanks again! :)
Seungbeom Kim | 30-Jul-13 at 12:39 pm | Permalink
Thanks for the clarification. However, in the last example, there’s no point in using `ls *.txt` when you can just say *.txt and also be safer and more correct. The former will actually split “annual report.txt” into two strings, “annual” and “report.txt”.