{"id":157,"date":"2016-06-03T12:02:24","date_gmt":"2016-06-03T20:02:24","guid":{"rendered":"http:\/\/www.gauntletwizard.net\/wordpress\/?p=157"},"modified":"2016-06-03T15:20:26","modified_gmt":"2016-06-03T23:20:26","slug":"bash-tips","status":"publish","type":"post","link":"https:\/\/www.gauntletwizard.net\/wordpress\/?p=157","title":{"rendered":"Bash tips."},"content":{"rendered":"<p>Here&#8217;s some things you should start most bash scripts with:<\/p>\n<pre>#!\/bin\/bash\r\n\r\nset -e\r\nset -x\r\nset -o pipefail\r\nset -u\r\n\r\n\r\nTMPDIR=$(mktemp -d)\r\ntrap 'rm -rf $TMPDIR' EXIT\r\n<\/pre>\n<p>Explanations of the lines:<\/p>\n<pre>#!\/bin\/bash<\/pre>\n<p>The shebang line is a unix convention that allows scripts to specify their interpreter. Since this is a bash script, we tell it to run this file with bash.<\/p>\n<pre>set -e<\/pre>\n<p>Exit immediately if any command fails. Makes it easy to spot when a script did not complete, and prevents things further down the line from doing the wrong thing because they were only partially setup.<\/p>\n<pre>set -x<\/pre>\n<p>Print each command as it&#8217;s run. It&#8217;s fantastically useful debug output, though some production scripts should have this disabled.<\/p>\n<pre>set -o pipefail<\/pre>\n<p>Exit with failure if any substage of a pipeline fails. This is about commands chained together with a pipe; e.g. If your grep command fails, the execution will fail, rather than simply outputting nothing to the next stage of the pipeline.<\/p>\n<pre>set -u<\/pre>\n<p>Makes referencing unset variables an error. <\/p>\n<p>Further explaination of the above three can be found in the <a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/The-Set-Builtin.html\">Bash Reference Manual entry on Set<\/a>.<\/p>\n<pre>TMPDIR=$(mktemp -d)\r\ntrap 'rm -rf $TMPDIR' EXIT<\/pre>\n<p>Create a scratch dir, automatically delete it when you&#8217;re done. It&#8217;s often useful to comment out the trap line during debugging. <\/p>\n<p>See also <a href=\"http:\/\/www.pixelbeat.org\/programming\/shell_script_mistakes.html\">Pixelbeat&#8217;s blog on Common shell script mistakes<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s some things you should start most bash scripts with: #!\/bin\/bash set -e set -x set -o pipefail set -u TMPDIR=$(mktemp -d) trap &#8216;rm -rf $TMPDIR&#8217; EXIT Explanations of the lines: #!\/bin\/bash The shebang line is a unix convention that allows scripts to specify their interpreter. Since this is a bash script, we tell it &hellip; <a href=\"https:\/\/www.gauntletwizard.net\/wordpress\/?p=157\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Bash tips.<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/157"}],"collection":[{"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":3,"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gauntletwizard.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}