{"id":174068,"date":"2021-02-09T16:00:27","date_gmt":"2021-02-09T13:00:27","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/"},"modified":"2021-02-09T16:00:27","modified_gmt":"2021-02-09T13:00:27","slug":"how-to-use-case-esac-statements-in-bash-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/","title":{"rendered":"#How to Use case .. esac Statements in Bash \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e4411c821f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e4411c821f\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/#Bash_case_esac_Statements_in_Bash\" >Bash case .. esac Statements in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/#A_simple_case_esac_script\" >A simple case .. esac script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/#Why_case_esac_And_Not_if\" >Why case .. esac And Not if?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/#Expanding_Our_Case\" >Expanding Our Case<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-esac-statements-in-bash-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use case .. esac Statements in Bash \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4038\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/f1fee0a0a83b16d260ba2e862cb46eec\/p\/uploads\/2017\/07\/add8ac45.png\" alt=\"Bash Shell\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this tutorial we look at Bash case based conditional statements. We review practical examples, and discuss when a Bash case .. esac statement is best employed over a more traditional <code>if .. then .. fi<\/code> statement.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Bash_case_esac_Statements_in_Bash\"><\/span>Bash case .. esac Statements in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Natively available from within the Bash shell, case conditional statements, formulated using the <code>case<\/code> and <code>esac<\/code> idioms (an idiom is a word or a group of words, or a keyword, with established usage patterns; a form of expressing a computer language), one can construct a complex conditional case-by-case (hence the term <code>case<\/code>) lookup and execution-per-case statement.<\/p>\n<p>The lookup part of a case based statement is sometimes called a <i>select<\/i>, leading to the term <i>select\/case statement<\/i> (or the reverse), used in some other coding languages like VBA, to refer to the same type of programming paradigm. Yet another set of languages (like C#, C++, Java and Java<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>) call this a <i>switch<\/i> or <i>switch\/case<\/i> statement. All these have similar meanings and operations.<\/p>\n<p>Let\u2019s build our first <code>case .. esac<\/code> statement using a simple script.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"A_simple_case_esac_script\"><\/span>A simple <i>case .. esac<\/i> script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We define our test script <code>test.sh<\/code> as follows:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncase \"${1}\" in &#13;\n  1) echo \"Option was 1!\";;&#13;\n  2) echo \"Option was 2!\";;&#13;\n  *) echo \"Option was something else: '${1}'\";;&#13;\nesac&#13;\n<\/pre>\n<p>Next, we make our script executable by executing <code>chmod +x test.sh<\/code> and then we execute the script itself, each time passing a different option as input.<\/p>\n<p>In our case opening clause, we specify that we want to use the first input option (<code>$1<\/code>) as the variable to be evaluated. Our <code>case .. esac<\/code> statement will verify each option (as indicated by the text preceding a <code>)<\/code> clause closing idiom) and then process the code block (after the <code>)<\/code> idiom) for the correct result.<\/p>\n<p>Note that there are two <code>;<\/code> end-of-statement terminators at the end of each line. The reason for this is simple; normal Bash syntax requires you to have at least one terminating idiom <code>;<\/code> (unless you are at the end of a line already), but inside the <code>case .. esac<\/code> statement, we need an additional <code>;<\/code> to indicate that we are terminating a code block for a specific case code execution block.<\/p>\n<p>We can see how we have defined code for both the cases where <code>1<\/code> and <code>2<\/code> are passed to the script, respectively. However, what happens when we pass something else? In that case, we want to jump to our <i>other<\/i> clause, indicated by a universal <code>*<\/code> idiom.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9467\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/08f44850fe24c8840f881b49052975e3\/p\/uploads\/2021\/02\/4d91bbee.png\" alt=\"A simple case .. esac statement in Bash\" width=\"450\" height=\"339\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We see how we pass <code>1<\/code> or <code>2<\/code> to the script, the corresponding correct action is taken; <code>Option was 1\/2!<\/code>. We also see how if we do something else, the <code>*<\/code> code block is executed correctly, and nicely our first passed variable to the script is displayed also. What would happen if we passed no option to our script? The <code>case .. esac<\/code> code would still be evaluated, and since nothing matches except for our catch-all <code>*<\/code> clause, that one is executed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9468\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ea23d889001a1cd7bd6e48562ba0dd90\/p\/uploads\/2021\/02\/b76675b0.png\" alt=\"Not passing a variable to our test script also works due to the * clause in our case .. esac statement\" width=\"253\" height=\"50\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>So what would happen if we were to assume, something which may seem logical, that we can leave off a single <code>;<\/code> statement from the end of the line of each <code>case .. esac<\/code> clause simply because at the same time an EOL (end of line) is reached?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9469\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/482cba541621e4c30eb9167f91b5e3b7\/p\/uploads\/2021\/02\/b79a6a80.png\" alt=\"Incorrect code block termination in a Bash case..esac statement\" width=\"513\" height=\"254\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Note that only a single terminating <code>;<\/code> was used in this script. Bash does not allow this, and an error is produced; the final terminating syntax of <code>;;<\/code> is required, and something to keep in mind when writing <code>case .. esac<\/code> statements<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Why_case_esac_And_Not_if\"><\/span>Why <i>case .. esac<\/i> And Not <i>if<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We could also write out the previous code using an <code>if<\/code> statement:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nif [ \"${1}\" == \"1\" ]; then&#13;\n  echo \"Option was 1!\"&#13;\nelif [ \"${1}\" == \"2\" ]; then&#13;\n  echo \"Option was 2!\"&#13;\nelse&#13;\n  echo \"Option was something else: '${1}'\";&#13;\nfi&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9470\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ce6e8252bf0c26193d50c665bf6339db\/p\/uploads\/2021\/02\/a253d008.png\" alt=\"An alternative to a case..esac statement using an if..then..elif..else..fi statement\" width=\"389\" height=\"339\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This looks comparable in code quality and shortness in comparison with our <code>case .. esac<\/code> code, and in the given situations and specific use cases like these, often an <code>if .. then .. elif .. else .. fi<\/code> based solution will do just fine.<\/p>\n<p>That said, if and when the complexity of the selection becomes more complex or larger, or if there are multi-layered case selections to be performed (note that <code>case .. esac<\/code> can be nested), it may pay off to use <code>case .. esac<\/code> statements instead.<\/p>\n<p>Another nice feature of <code>case .. esac<\/code> statements is that one can list two possible options for a single code execution block, as we will see in our next example, however this an also be done with standard Bash <code>if<\/code> statements, using for example the <code>-a<\/code> (and) and <code>-o<\/code> (or) options from within our test clause and specifying a secondary test clause. For example, <code>if [ \"${1}\" -eq 1 -o \"${1}\" -eq 3 ]; then ...<\/code> Note that here we also used numerical comparisons instead of text based comparisons as in our example above.<\/p>\n<p>For more information on <code>if .. then .. else .. elif .. fi<\/code> conditionals, you can also read our article Conditional Testing in Bash: if, then, else, elif.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Expanding_Our_Case\"><\/span>Expanding Our Case<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let us expand on our previous script and make our conditions a bit more complex:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ncase \"${1}\" in &#13;\n  1|3) echo \"Option was 1!\"&#13;\n       echo \"Or, option was 3!\";;&#13;\n  2|'a') case \"${1}\" in &#13;\n           2) echo \"Option was 2!\";;&#13;\n         'a') echo \"Option was 'a'!\";;&#13;\n         esac;;&#13;\n  *) echo \"Option was something else: '${1}'\";;&#13;\nesac&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9471 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/35525919718b12880efcf9bda41de781\/p\/uploads\/2021\/02\/9a037cda.png\" alt=\"A more complex case .. esac statement in Bash with additional selectors\" width=\"456\" height=\"511\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this case, we have listed two possible options for each code block and separated them by a <code>|<\/code> separator. Note how everything works well and the inputs <code>1<\/code> and <code>3<\/code> are correctly parsed. In the second code block we have created a nested case, and used each possible input to the second code block (namely <code>2<\/code> and <code>a<\/code>) as new options in the nested\/secondary esac.<\/p>\n<p>This again works flawlessly. The flow through the code is that in the first <code>case<\/code> statement, the <code>2|'a'<\/code> branch is taken is <code>2<\/code> or <code>a<\/code> is passed, and then the second <code>case<\/code> ensures that either <code>2<\/code> or <code>a<\/code> is selected individually. Of interest is also the use of <code>;;<\/code> every time we want to terminate a code block for a specific option, and this also applies to terminating the nested <code>case .. esac<\/code>, again with <code>;;<\/code>, i.e. <code>esac;;<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this tutorial, we looked at practical examples of Bash based <code>case .. esac<\/code> statements. We also saw where case statements are a better choice then more traditional <code>if .. then .. fi<\/code> based statements.<\/p>\n<p>If Bash interests you, you may find Primer: Bash Loops: for, while, and until, Bash Process Termination Hacks and How to Correctly Parse File Names in Bash to be of interest.<\/p>\n<p><strong>Enjoy!<\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9466\/how-to-use-case-esac-statements-in-bash\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use case .. esac Statements in Bash \u2013 CloudSavvy IT&#8221; In this tutorial we look at Bash case based conditional statements. We review practical examples, and discuss when a Bash case .. esac statement is best employed over a more traditional if .. then .. fi statement. Bash case .. esac Statements in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":174069,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/f1fee0a0a83b16d260ba2e862cb46eec\/p\/uploads\/2017\/07\/add8ac45.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-174068","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/174068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=174068"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/174068\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/174069"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=174068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=174068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=174068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}