{"id":26923,"date":"2016-11-21T08:29:54","date_gmt":"2016-11-21T06:29:54","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=26923"},"modified":"2016-11-21T08:29:54","modified_gmt":"2016-11-21T06:29:54","slug":"shell-parameter-expansion-default-values-for-shell-script-parameters","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/21\/shell-parameter-expansion-default-values-for-shell-script-parameters\/","title":{"rendered":"Shell parameter expansion : default values for shell script parameters"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>When writing shell scripts, it&#8217;s often useful to accept some command line parameters. \u00a0It&#8217;s even more useful to have some defaults for those parameters. \u00a0Until now I&#8217;ve been using if statements to check if the parameter was empty, and if it was, to set it to the default value. \u00a0Something like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\nDB_HOST=$1\r\nDB_NAME=$2\r\nDB_USER=$3\r\nDB_PASS=$4\r\n\r\nif &#x5B; -z &quot;$DB_HOST&quot; ]\r\nthen\r\n    DB_HOST=&quot;localhost&quot;\r\nfi\r\n\r\nif &#x5B; -z &quot;$DB_NAME&quot; ]\r\nthen\r\n    DB_NAME=&quot;wordpress&quot;\r\nfi\r\n\r\nif &#x5B; -z &quot;$DB_USER&quot; ]\r\nthen\r\n    DB_USER=&quot;root&quot;\r\nfi\r\n\r\necho &quot;Connecting to the database:&quot;\r\necho &quot;Host: $DB_HOST&quot;\r\necho &quot;Name: $DB_NAME&quot;\r\necho &quot;User: $DB_USER&quot;\r\necho &quot;Pass: $DB_PASS&quot;\r\n<\/pre>\n<p>It turns out there is a much more elegant way to do this with <a href=\"http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Shell-Parameter-Expansion.html\">shell parameter expansion<\/a>. \u00a0Here is how it looks rewritten:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\nDB_HOST=${1-localhost}\r\nDB_NAME=${2-wordpress}\r\nDB_USER=${3-root}\r\nDB_PASS=$4\r\n\r\necho &quot;Connecting to the database:&quot;\r\necho &quot;Host: $DB_HOST&quot;\r\necho &quot;Name: $DB_NAME&quot;\r\necho &quot;User: $DB_USER&quot;\r\necho &quot;Pass: $DB_PASS&quot;\r\n<\/pre>\n<p>This is so much better. Not only the script itself is shorter, but it&#8217;s also much more obvious what is going on. \u00a0Copy-paste errors are much less likely to happen here too.<\/p>\n<p>I wish I learned about this sooner.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>When writing shell scripts, it&#8217;s often useful to accept some command line parameters. \u00a0It&#8217;s even more useful to have some defaults for those parameters. \u00a0Until now I&#8217;ve been using if statements to check if the parameter was empty, and if it was, to set it to the default value. \u00a0Something like this: #!\/bin\/bash DB_HOST=$1 DB_NAME=$2 &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/21\/shell-parameter-expansion-default-values-for-shell-script-parameters\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Shell parameter expansion : default values for shell script parameters<\/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":"Shell parameter expansion : default values for shell script parameters #Linux #shell #bash #programming #SysAdmin","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,6,18,133,62],"tags":[3098,1960],"keyring_services":[],"class_list":["post-26923","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","category-programming","category-sysadmin","category-technology","tag-bash","tag-command-line"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":13075,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/08\/03\/convert-wordpress-tag-to-existing-category\/","url_meta":{"origin":26923,"position":0},"title":"Convert WordPress tag to existing category","author":"Leonid Mamchenkov","date":"August 3, 2010","format":false,"excerpt":"By now most people have figured out what is the difference between categories and tags, and which ones to use where. \u00a0But that doesn't necessarily mean that the problems are over. \u00a0Once in a while there is a need to convert from one to another. WordPress provides the tool to\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":16526,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/07\/20\/wp-config-php-and-git\/","url_meta":{"origin":26923,"position":1},"title":"wp-config.php and git","author":"Leonid Mamchenkov","date":"July 20, 2012","format":false,"excerpt":"If you are storing your WordPress changes in git and then deploy the project between different machines (local, test server, production environment, etc), then you are probably familiar with a problem of wp-config.php file. \u00a0WordPress uses it for things like database credentials, which vary from machine to machine. \u00a0But you\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":16696,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/09\/10\/managing-gettext-translations-on-the-command-line\/","url_meta":{"origin":26923,"position":2},"title":"Managing gettext translations on the command line","author":"Leonid Mamchenkov","date":"September 10, 2012","format":false,"excerpt":"I am working on a rather multilingual project in the office currently. \u00a0And, as always, we tried a few alternatives before ending up with gettext again. \u00a0For those of you who don't know, gettext is the de facto standard for managing language translations in software, especially when it comes to\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":26130,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/05\/26\/explain-shell\/","url_meta":{"origin":26923,"position":3},"title":"Explain Shell","author":"Leonid Mamchenkov","date":"May 26, 2016","format":false,"excerpt":"Here's a good resource for all of those who is trying to learn shell and\/or figure out complex commands with lots of parameters and pipes - Explain Shell. You just paste the command and hit the \"Explain\" button, and the site will decompose the command into parts, providing relevant parts\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"ExplainShell","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/05\/ExplainShell-500x320.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":29107,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/12\/18\/bash-parameter-expansion\/","url_meta":{"origin":26923,"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":27447,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/22\/bulletproof-bash-stop-script-on-error\/","url_meta":{"origin":26923,"position":5},"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":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26923","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=26923"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26923\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=26923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=26923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=26923"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=26923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}