{"id":378154,"date":"2021-12-08T16:00:47","date_gmt":"2021-12-08T13:00:47","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-case-statements-in-bash-scripts\/"},"modified":"2021-12-08T16:00:47","modified_gmt":"2021-12-08T13:00:47","slug":"how-to-use-case-statements-in-bash-scripts","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-statements-in-bash-scripts\/","title":{"rendered":"#How to Use Case Statements in Bash Scripts"},"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-6a2d7e0732c24\" 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-6a2d7e0732c24\" 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-statements-in-bash-scripts\/#The_case_Statement\" >The case Statement<\/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-statements-in-bash-scripts\/#The_Generic_case\" >The Generic case<\/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-statements-in-bash-scripts\/#A_Simple_Example\" >A Simple Example<\/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-statements-in-bash-scripts\/#Using_Multiple_Patterns_in_a_Clause\" >Using Multiple Patterns in a Clause<\/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-statements-in-bash-scripts\/#Using_Digits_In_case_Statements\" >Using Digits In case Statements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-statements-in-bash-scripts\/#Using_case_Statements_in_for_Loops\" >Using case Statements in for Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-statements-in-bash-scripts\/#Handling_Exit_Codes_With_case_Statements\" >Handling Exit Codes With case Statements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-case-statements-in-bash-scripts\/#Legibility_Helps_Maintainability\" >Legibility Helps Maintainability<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use Case Statements in Bash <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>s&#8221;<\/strong><\/p>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage wp-image-767003 size-full\" data-pagespeed-lazy-srcset=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/chalboard-sorting-shapes.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/chalboard-sorting-shapes.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/chalboard-sorting-shapes.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Sorting shapes into categories on a chalkboard\" width=\"1200\" height=\"675\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/hand-drawing-geometry-categorize-on-chalkboard-602143955\" data-credittext=\"Patpitchaya\/Shutterstock.com\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/hand-drawing-geometry-categorize-on-chalkboard-602143955\">Patpitchaya\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Bash case statements are powerful yet easy to write. When you revisit an old Linux script you\u2019ll be glad you used a <code>case<\/code> statement instead of a long <code>if-then-else<\/code> statement.<\/p>\n<h2 id=\"the-case-statement\"><span class=\"ez-toc-section\" id=\"The_case_Statement\"><\/span>The case Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Most programming languages have their version of a <code>switch<\/code> or <code>case<\/code> statement. These direct the flow of program execution according to the value of a variable. Typically, there is a branch of execution defined for each of the expected possible values of the variable and one catch-all or\u00a0<em>default<\/em>\u00a0branch for all other values.<\/p>\n<p>The logical functionality is similar to a long sequence of <code>if-then<\/code> statements with an <code>else<\/code> statement catching everything that hasn\u2019t been previously handled by one of the <code>if<\/code> statements.<\/p>\n<p>The Bash implementation of <code>case<\/code>\u00a0tries to match an\u00a0<em>expression<\/em>\u00a0with one of the clauses. It does this by looking at each clause, in turn, trying to find a matching <em>pattern<\/em>. Patterns in clauses are strings, but\u2014counterintuitively\u2014that doesn\u2019t mean we can\u2019t use numerical values as the expression.<\/p>\n<h2 id=\"the-generic-case\"><span class=\"ez-toc-section\" id=\"The_Generic_case\"><\/span>The Generic case<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The generic form of the <code>case<\/code> statement is this:<\/p>\n<pre>case expression in &#13;\n&#13;\n\u00a0 pattern-1)&#13;\n  \u00a0 statement &#13;\n\u00a0   ;;&#13;\n&#13;\n  pattern-2) &#13;\n\u00a0   statement&#13;\n\u00a0   ;;&#13;\n  \u00a0 .&#13;\n\u00a0   .&#13;\n  \u00a0 .&#13;\n&#13;\n  pattern-N) &#13;\n\u00a0   statement &#13;\n\u00a0   ;;&#13;\n&#13;\n  *) &#13;\n\u00a0   statement &#13;\n\u00a0   ;; &#13;\nesac&#13;\n&#13;\n<\/pre>\n<ul>\n<li>A <code>case<\/code> statement must start with the <code>case<\/code> keyword and end with the <code>esac<\/code> keyword.<\/li>\n<li>The expression is evaluated and compared with the patterns in each\u00a0<em>clause<\/em>\u00a0until a match is found.<\/li>\n<li>The statement or statements in the matching clause are executed.<\/li>\n<li>A double semicolon \u201c<code>;;<\/code>\u201d is used to terminate a clause.<\/li>\n<li>If a pattern is matched and the statements in that clause executed, all other patterns are ignored.<\/li>\n<li>There is no limit to the number of clauses.<\/li>\n<li>An asterisk \u201c<code>*<\/code>\u201d denotes the default pattern. If an expression isn\u2019t matched with any of the other patterns in the <code>case<\/code> statement the default clause is executed.<\/li>\n<\/ul>\n<h2 id=\"a-simple-example\"><span class=\"ez-toc-section\" id=\"A_Simple_Example\"><\/span>A Simple Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This script tells us the opening hours for an imaginary shop. It uses the <code>date<\/code> command with the <code>+\"%a\"<\/code> format string to obtain the shortened day name. This is stored in the <code>DayName<\/code> variable.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nDayName=$(date +\"%a\")&#13;\n&#13;\necho \"Opening hours for $DayName\"&#13;\n&#13;\ncase $DayName in&#13;\n&#13;\n  Mon)&#13;\n    echo \"09:00 - 17:30\"&#13;\n    ;;&#13;\n&#13;\n  Tue)&#13;\n    echo \"09:00 - 17:30\"&#13;\n    ;;&#13;\n&#13;\n  Wed)&#13;\n    echo \"09:00 - 12:30\"&#13;\n    ;;&#13;\n&#13;\n  Thu)&#13;\n    echo \"09:00 - 17:30\"&#13;\n    ;;&#13;\n&#13;\n  Fri)&#13;\n    echo \"09:00 - 16:00\"&#13;\n    ;;&#13;\n&#13;\n  Sat)&#13;\n    echo \"09:30 - 16:00\"&#13;\n    ;;&#13;\n&#13;\n  Sun)&#13;\n    echo \"Closed all day\"&#13;\n    ;;&#13;\n&#13;\n  *)&#13;\n    ;;&#13;\nesac<\/pre>\n<p>Copy that text into an editor and save it as a file called \u201copen.sh.\u201d<\/p>\n<p>We\u2019ll need to use the <code>chmod<\/code> command to make it executable. You\u2019ll need to do that for all of the scripts you create as you work through this article.<\/p>\n<pre>chmod +x open.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766981\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Making the open.sh script executable\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can now run our script.<\/p>\n<pre>.\/open.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766982\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the open.sh script\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The day the screenshot was taken happens to be a Friday. That means the <code>DayName<\/code>\u00a0variable holds the string \u201cFri.\u201d This is matched with the \u201cFri\u201d pattern of the \u201cFri)\u201d clause.<\/p>\n<p>Note that the patterns in the clauses don\u2019t need to be wrapped in double quotes, but it doesn\u2019t do any harm if they are. However, you <em>must<\/em> use double quotes if the pattern contains spaces.<\/p>\n<p>The default clause has been left empty. Anything that doesn\u2019t match one of the preceding clauses is ignored.<\/p>\n<p>That script works and it is easy to read, but it is long-winded and repetitive. We can shorten that type of\u00a0<code>case<\/code>\u00a0statement quite easily.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Use the chmod Command on Linux<\/em><\/strong><\/p>\n<h2 id=\"multiple-patterns\"><span class=\"ez-toc-section\" id=\"Using_Multiple_Patterns_in_a_Clause\"><\/span>Using Multiple Patterns in a Clause<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A really neat feature of <code>case<\/code> statements is you can use multiple patterns in each clause. If the expression matches any of those patterns the statements in that clause are executed.<\/p>\n<p>Here\u2019s a script that tells you how many days there are in a month. There can only be three answers: 30 days, 31 days, or 28 or 29 days for February.\u00a0So, although there are 12 months we only need three clauses.<\/p>\n<p>In this script, the user is prompted for the name of a month. To make the pattern matching case insensitive we use the <code>shopt<\/code> command with the <code>-s nocasematch<\/code> option. It won\u2019t matter if the input contains uppercase, lowercase, or a mixture of the two.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nshopt -s nocasematch&#13;\n&#13;\necho \"Enter name of a month\"&#13;\nread month&#13;\n&#13;\ncase $month in&#13;\n&#13;\n  February)&#13;\n    echo \"28\/29 days in $month\"&#13;\n    ;;&#13;\n&#13;\n  April | June | September | November)&#13;\n    echo \"30 days in $month\"&#13;\n    ;;&#13;\n&#13;\n  January | March | May | July | August | October | December)&#13;\n    echo \"31 days in $month\"&#13;\n    ;;&#13;\n&#13;\n  *)&#13;\n    echo \"Unknown month: $month\"&#13;\n    ;;&#13;\nesac<\/pre>\n<p>February gets a clause to itself, and all the other months share two clauses according to whether they have 30 or 31 days in them. Multi-pattern clauses use the pipe symbol \u201c|\u201d as the separator. The default case catches badly spelled months.<\/p>\n<p>We saved this into a file called \u201cmonth.sh\u201d, and made it executable.<\/p>\n<pre>chmod +x month.sh<\/pre>\n<p>We\u2019ll run the script several times and show that it doesn\u2019t matter if we use uppercase or lowercase.<\/p>\n<pre>.\/month.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766988\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the month.sh script with different case inputs\" width=\"644\" height=\"355\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Because we told the script to ignore differences in uppercase and lowercase any month name spelled correctly is handled by one of the three main clauses. Badly spelled months are caught by the default clause.<\/p>\n<h2 id=\"using-digits-in-case-statements\"><span class=\"ez-toc-section\" id=\"Using_Digits_In_case_Statements\"><\/span>Using Digits In case Statements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can also use digits or numerical variables as the expression. This script asks the user to enter a number in the range 1..3.\u00a0 To make it clear that the patterns in each clause are strings, they\u2019ve been wrapped in double quotes. Despite this, the script still matches the user\u2019s input to the appropriate clause.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho \"Enter 1, 2, or 3: \"&#13;\nread Number&#13;\n&#13;\ncase $Number in&#13;\n&#13;\n  \"1\")&#13;\n    echo \"Clause 1 matched\"&#13;\n    ;;&#13;\n&#13;\n  \"2\")&#13;\n    echo \"Clause 2 matched\"&#13;\n    ;;&#13;\n&#13;\n  \"3\")&#13;\n    echo \"Clause 3 matched\"&#13;\n    ;;&#13;\n&#13;\n  *)&#13;\n    echo \"Default clause matched\"&#13;\n    ;;&#13;\nesac<\/pre>\n<p>Save this into a file called \u201cnumber.sh\u201d, make it executable, and then run it:<\/p>\n<pre>.\/number.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766990\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the number.sh script and testing different user inputs\" width=\"644\" height=\"280\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"using-case-statements-in-for-loops\"><span class=\"ez-toc-section\" id=\"Using_case_Statements_in_for_Loops\"><\/span>Using case Statements in for Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A <code>case<\/code> statement tries to pattern match a single expression. If you have a lot of expressions to process, you can put the <code>case<\/code> statement inside a <code>for<\/code> loop.<\/p>\n<p>This script executes the <code>ls<\/code> command to get a list of files. In the <code>for<\/code> loop, file globbing\u2014similar but different to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.cloudsavvyit.com\/921\/how-do-you-actually-use-regex\/\">regular expressions<\/a>\u2014is applied to each file in turn to extract the file extension. This is stored in the <code>Extension<\/code> string variable.<\/p>\n<p>The <code>case<\/code> statement uses the <code>Extension<\/code> variable as the expression it tries to match to a clause.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nfor File in $(ls)&#13;\n&#13;\ndo&#13;\n  # extract the file extension&#13;\n  Extension=${File##*.}&#13;\n&#13;\n  case \"$Extension\" in&#13;\n&#13;\n    sh)&#13;\n      echo \" Shell script: $File\"&#13;\n      ;;&#13;\n&#13;\n    md)&#13;\n      echo \" Markdown file: $File\"&#13;\n      ;;&#13;\n&#13;\n    png)&#13;\n      echo \"PNG image file: $File\"&#13;\n      ;;&#13;\n&#13;\n    *)&#13;\n      echo \"Unknown: $File\"&#13;\n      ;;&#13;\n  esac&#13;\ndone<\/pre>\n<p>Save this text into a file called \u201cfiletype.sh\u201d, make it executable, and then run it using:<\/p>\n<pre>.\/filetype.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766993\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the filetype.sh script and identifying files\" width=\"644\" height=\"230\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Our minimalist file type identification script works.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Use &#8220;Here Documents&#8221; in Bash on Linux<\/em><\/strong><\/p>\n<h2 id=\"using-exit-codes-with-case-statements\"><span class=\"ez-toc-section\" id=\"Handling_Exit_Codes_With_case_Statements\"><\/span>Handling Exit Codes With case Statements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A well-behaved program will send an exit code to the shell when it terminates. The conventional scheme uses an exit code value of zero to indicate a problem-free execution, and values of one or more to indicate different types of error.<\/p>\n<p>Many programs use only zero and one. Lumping all error conditions into a single exit code makes identifying problems more difficult, but it is common practice.<\/p>\n<p>We created a small program called \u201cgo-geek\u201d that would randomly return exit codes of zero or one. This next script calls <code>go-geek<\/code>. It acquires the exit code using the <code>$?<\/code> shell variable and uses that as the expression for the <code>case<\/code> statement.<\/p>\n<p>A real-world script would do appropriate processing according to the success or failure of the command that generated the exit code.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\ngo-geek&#13;\n&#13;\ncase $? in&#13;\n&#13;\n  \"0\")&#13;\n    echo \"Response was: Success\"&#13;\n    echo \"Do appropriate processing in here\"&#13;\n    ;;&#13;\n&#13;\n  \"1\")&#13;\n    echo \"Response was: Error\"&#13;\n    echo \"Do appropriate error handling in here\"&#13;\n    ;;&#13;\n&#13;\n  *)&#13;\n    echo \"Unrecognised response: $?\"&#13;\n    ;;&#13;\nesac<\/pre>\n<p>Save this into a script called \u201creturn-code.sh\u201d and make it executable. You\u2019ll need to substitute some other command for our <code>go-geek<\/code> command. You could try to <code>cd<\/code> into a directory that doesn\u2019t exist to get an exit code of one, and then edit your script to <code>cd<\/code> to an accessible directory to get an exit code of zero.<\/p>\n<p>Running the script a few times shows the different exit codes being correctly identified by the <code>case<\/code> statement.<\/p>\n<pre>.\/return-code.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766997\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the return-code.sh script showing the handling of different exit codes\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"wrap-up\"><span class=\"ez-toc-section\" id=\"Legibility_Helps_Maintainability\"><\/span>Legibility Helps Maintainability<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Going back to old Bash scripts and working out how they do what they do, especially if they were written by someone else, is challenging. Amending the functionality of old scripts is even harder.<\/p>\n<p>The <code>case<\/code> statement gives you branching logic with clear and easy syntax. That\u2019s a win-win.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Install and Use the Linux Bash Shell on Windows 10<\/em><\/strong><\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\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.howtogeek.com\/766978\/how-to-use-case-statements-in-bash-scripts\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use Case Statements in Bash Scripts&#8221; Patpitchaya\/Shutterstock.com Bash case statements are powerful yet easy to write. When you revisit an old Linux script you\u2019ll be glad you used a case statement instead of a long if-then-else statement. The case Statement Most programming languages have their version of a switch or case statement. These&#8230;<\/p>\n","protected":false},"author":1,"featured_media":378155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/chalboard-sorting-shapes.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-378154","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\/378154","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=378154"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/378154\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/378155"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=378154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=378154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=378154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}