{"id":50,"date":"2006-11-26T15:00:39","date_gmt":"2006-11-26T19:00:39","guid":{"rendered":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/using-quotation-marks-effectively-in-unix\/"},"modified":"2007-02-13T00:45:57","modified_gmt":"2007-02-13T04:45:57","slug":"using-quotation-marks-effectively-in-unix","status":"publish","type":"post","link":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/using-quotation-marks-effectively-in-unix\/","title":{"rendered":"Using quotation marks effectively in Unix"},"content":{"rendered":"

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 `<\/code> 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:<\/p>\r\n\r\n

\r\n
Single quote\/forward quote '<\/code><\/dt>
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';<\/code> will return This is $x<\/code>. Unix did not replace the variable $x<\/code> with 17.\r\n<\/dd>\r\n
Double quote \"<\/code><\/dt>
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<\/code> will search hamlet.txt for the famous line. In contrast, grep To be or not to be hamlet.txt<\/code> searches for To<\/code> in the files be, or, not, to, be, hamlet.txt<\/code> (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\";<\/code> will return This is 17<\/code>.<\/dd>\r\n
Backtick\/back quote `<\/code><\/dt>
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 $()<\/code>. For example: echo \"You are in `pwd`\"<\/code> will return You are in \/usr\/bin\/<\/code> 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<\/code><\/dd>\r\n<\/dl> \r\n

Knowing how these are used can really help with using Unix. The difference between 'commands'<\/code> and \"commands\"<\/code> 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.<\/p>.","protected":false},"excerpt":{"rendered":"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 […]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[120,121,706,119,705,30,117,115,116,118,29],"_links":{"self":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}