Using quotation marks effectively in Unix

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 return This 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 for To in the files be, 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 return This 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 return You 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.

.