{"id":27291,"date":"2017-02-05T09:51:32","date_gmt":"2017-02-05T07:51:32","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=27291"},"modified":"2017-02-05T09:51:32","modified_gmt":"2017-02-05T07:51:32","slug":"defensive-bash-programming","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/05\/defensive-bash-programming\/","title":{"rendered":"Defensive BASH Programming"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>If you write any Bash code that lasts more than a day, you should definitely read &#8220;<a href=\"http:\/\/www.kfirlavi.com\/blog\/2012\/11\/14\/defensive-bash-programming\/\">Defensive BASH Programming<\/a>&#8221; and follow the advice, if you haven&#8217;t already. \u00a0It covers the following:<\/p>\n<ul>\n<li>Immutable global variables<\/li>\n<li>Everything is local<\/li>\n<li>main()<\/li>\n<li>Everything is a function<\/li>\n<li>Debugging functions<\/li>\n<li>Code clarity<\/li>\n<li>Each line does just one thing<\/li>\n<li>Printing usage<\/li>\n<li>Command line arguments<\/li>\n<li>Unit Testing<\/li>\n<\/ul>\n<p>All that with code examples and explanation of importance.<\/p>\n<p>&nbsp;<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>If you write any Bash code that lasts more than a day, you should definitely read &#8220;Defensive BASH Programming&#8221; and follow the advice, if you haven&#8217;t already. \u00a0It covers the following: Immutable global variables Everything is local main() Everything is a function Debugging functions Code clarity Each line does just one thing Printing usage Command &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/05\/defensive-bash-programming\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Defensive BASH Programming<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Defensive BASH Programming #Bash #programming #SysAdmin #shell","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,133,62],"tags":[3098,3069,1960],"keyring_services":[],"class_list":["post-27291","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-sysadmin","category-technology","tag-bash","tag-best-practices","tag-command-line"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":7918,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/09\/17\/bash-prompts\/","url_meta":{"origin":27291,"position":0},"title":"Bash prompts","author":"Leonid Mamchenkov","date":"September 17, 2004","format":false,"excerpt":"Maintainer of the Bash Prompt HOWTO, it turns out, also keeps a small website with examlpes of different bash prompts. It provides screenshots together with code for some really wicked prompts. If you are into that kind of things, but all of ideas, than check the site out. Maybe you'll\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27447,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/22\/bulletproof-bash-stop-script-on-error\/","url_meta":{"origin":27291,"position":1},"title":"Bulletproof Bash : Stop script on error","author":"Leonid Mamchenkov","date":"March 22, 2017","format":false,"excerpt":"The other day I've been puzzled by the results of a cron job script. \u00a0The bash script in question was written in a hurry a while back, and I was under the assumption that if any of its steps fail, the whole script will fail. \u00a0I was wrong. \u00a0Some commands\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":23357,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/01\/21\/shellfire-bash-sh-dash-function-libraries\/","url_meta":{"origin":27291,"position":2},"title":"shellfire &#8211; namespaced, composable shell (bash, sh and dash) function libraries","author":"Leonid Mamchenkov","date":"January 21, 2015","format":"link","excerpt":"shellfire - namespaced, composable shell (bash, sh and dash) function libraries","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28622,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/06\/11\/safe-ways-to-do-things-in-bash\/","url_meta":{"origin":27291,"position":3},"title":"Safe ways to do things in bash","author":"Leonid Mamchenkov","date":"June 11, 2018","format":false,"excerpt":"\"Safe ways to do things in bash\" is yet another guide to some of the best practices for writing bash scripts.\u00a0 It covers all the usual bits of quoting, escaping, error handling, and more.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29107,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/12\/18\/bash-parameter-expansion\/","url_meta":{"origin":27291,"position":4},"title":"Bash parameter expansion","author":"Leonid Mamchenkov","date":"December 18, 2018","format":false,"excerpt":"If you've ever written a bash script with variables, and know that it wasn't your last one,\u00a0 I promise you, you'll love this wiki page.\u00a0 It covers a whole lot of different ways to expand and manipulate variable values in bash, all on a single, conveniently organized page.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8473,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/01\/13\/working-more-productively-with-bash-2x3x\/","url_meta":{"origin":27291,"position":5},"title":"Working more productively with bash 2.x\/3.x","author":"Leonid Mamchenkov","date":"January 13, 2005","format":false,"excerpt":"This article is a must read for anyone who is using Bash shell. There are still probably a thing or two that you didn't know.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=27291"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27291\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=27291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=27291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=27291"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=27291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}