{"id":10509,"date":"2019-03-06T07:49:53","date_gmt":"2019-03-06T07:49:53","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=10509"},"modified":"2019-03-06T08:50:22","modified_gmt":"2019-03-06T08:50:22","slug":"bash-case-statement-linuxize","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/06\/bash-case-statement-linuxize\/","title":{"rendered":"Bash Case Statement | Linuxize"},"content":{"rendered":"<figure class=\"relative -mx-6 lg:mx-0 mb-6\">\n<div class=\"w-full mx-auto my-0 block relative\">\n<div><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-10519 alignleft\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2019\/03\/featured-300x159.jpg\" alt=\"\" width=\"300\" height=\"159\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2019\/03\/featured-300x159.jpg 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2019\/03\/featured-768x407.jpg 768w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2019\/03\/featured.jpg 1000w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/div>\n<div><\/div>\n<\/div>\n<\/figure>\n<div class=\"markdown\">\n<p>Bash case statements are generally used to simplify complex conditionals when you have multiple different choices. Using the case statement instead of nested if statements will help you make your bash scripts more readable and easier to maintain.<\/p>\n<p>The Bash case statement has a similar concept with the Javascript or C switch statement. The main difference is that unlike the C switch statement the Bash case statement doesn\u2019t continue to search for a pattern match once it has found one and executed statements associated with that pattern.<\/p>\n<p>In this tutorial, we will cover the basics of the Bash case statements and show you how to use them in your shell scripts.<span id=\"ezoic-pub-ad-placeholder-138\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<h2 id=\"case-statement-syntax\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/bash-case-statement\/#case-statement-syntax\" data-scroll=\"\">Case Statement Syntax<\/a><\/h2>\n<p>The Bash case statement takes the following form:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-sh\" data-lang=\"sh\"><span class=\"k\">case<\/span> EXPRESSION in\r\n\r\n  PATTERN_1<span class=\"o\">)<\/span>\r\n    STATEMENTS\r\n    <span class=\"p\">;;<\/span>\r\n\r\n  PATTERN_2<span class=\"o\">)<\/span>\r\n    STATEMENTS\r\n    <span class=\"p\">;;<\/span>\r\n\r\n  PATTERN_N<span class=\"o\">)<\/span>\r\n    STATEMENTS\r\n    <span class=\"p\">;;<\/span>\r\n\r\n  *<span class=\"o\">)<\/span>\r\n    STATEMENTS\r\n    <span class=\"p\">;;<\/span>\r\n<span class=\"k\">esac<\/span><\/code><\/pre>\n<p><span class=\"code-copy button main small\" data-clipboard-text=\"case EXPRESSION in\n\n  PATTERN_1)\n    STATEMENTS\n    ;;\n\n  PATTERN_2)\n    STATEMENTS\n    ;;\n\n  PATTERN_N)\n    STATEMENTS\n    ;;\n\n  *)\n    STATEMENTS\n    ;;\nesac\">Copy<\/span><\/div>\n<ul>\n<li>Each case statement starts with the\u00a0<code>case<\/code>\u00a0keyword followed by the case expression and the\u00a0<code>in<\/code>\u00a0keyword. The statement ends with the\u00a0<code>esac<\/code>\u00a0keyword.<\/li>\n<li>You can use multiple patterns separated by the\u00a0<code>|<\/code>\u00a0operator. The\u00a0<code>)<\/code>\u00a0operator terminates a pattern list.<\/li>\n<li>A pattern can have\u00a0<a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Pattern-Matching.html#Pattern-Matching\">special characters<\/a>.<\/li>\n<li>A pattern and its associated commands are known as a clause.<\/li>\n<li>Each clause must be terminated with\u00a0<code>;;<\/code>.<\/li>\n<li>The commands corresponding to the first pattern that matches the expression are executed.<\/li>\n<li>It is a common practice to use the wildcard asterisk symbol (<code>*<\/code>) as a final pattern to define the default case. This pattern will always match.<\/li>\n<li>If no pattern is matched the return status is zero. Otherwise, the return status is the exit status of the executed commands.<\/li>\n<\/ul>\n<h2 id=\"case-statement-example\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/bash-case-statement\/#case-statement-example\" data-scroll=\"\">Case Statement Example<\/a><\/h2>\n<p>Here is an example using the case statement in a bash script that will print the official language of a given country:<\/p>\n<\/div>\n<div class=\"code-label\">languages.sh<\/div>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-sh\" data-lang=\"sh\"><span class=\"cp\">#!\/bin\/bash\r\n<\/span>\r\n<span class=\"nb\">echo<\/span> -n <span class=\"s2\">\"Enter the name of a country: \"<\/span>\r\n<span class=\"nb\">read<\/span> COUNTRY\r\n\r\n<span class=\"nb\">echo<\/span> -n <span class=\"s2\">\"The official language of <\/span><span class=\"nv\">$COUNTRY<\/span><span class=\"s2\"> is \"<\/span>\r\n\r\n<span class=\"k\">case<\/span> <span class=\"nv\">$COUNTRY<\/span> in\r\n\r\n  Lithuania<span class=\"o\">)<\/span>\r\n    <span class=\"nb\">echo<\/span> -n <span class=\"s2\">\"Lithuanian\"<\/span>\r\n    <span class=\"p\">;;<\/span>\r\n\r\n  Romania <span class=\"p\">|<\/span> Moldova<span class=\"o\">)<\/span>\r\n    <span class=\"nb\">echo<\/span> -n <span class=\"s2\">\"Romanian\"<\/span>\r\n    <span class=\"p\">;;<\/span>\r\n\r\n  Italy <span class=\"p\">|<\/span> <span class=\"s2\">\"San Marino\"<\/span> <span class=\"p\">|<\/span> Switzerland <span class=\"p\">|<\/span> <span class=\"s2\">\"Vatican City\"<\/span><span class=\"o\">)<\/span>\r\n    <span class=\"nb\">echo<\/span> -n <span class=\"s2\">\"Italian\"<\/span>\r\n    <span class=\"p\">;;<\/span>\r\n\r\n  *<span class=\"o\">)<\/span>\r\n    <span class=\"nb\">echo<\/span> -n <span class=\"s2\">\"unknown\"<\/span>\r\n    <span class=\"p\">;;<\/span>\r\n<span class=\"k\">esac<\/span><\/code><\/pre>\n<p><span class=\"code-copy button main small\" data-clipboard-text=\"#!\/bin\/bash\n\necho -n &quot;Enter the name of a country: &quot;\nread COUNTRY\n\necho -n &quot;The official language of $COUNTRY is &quot;\n\ncase $COUNTRY in\n\n  Lithuania)\n    echo -n &quot;Lithuanian&quot;\n    ;;\n\n  Romania | Moldova)\n    echo -n &quot;Romanian&quot;\n    ;;\n\n  Italy | &quot;San Marino&quot; | Switzerland | &quot;Vatican City&quot;)\n    echo -n &quot;Italian&quot;\n    ;;\n\n  *)\n    echo -n &quot;unknown&quot;\n    ;;\nesac\">Copy<\/span><\/div>\n<p>Save the custom script as a file and run it from the command line.<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">bash languages.sh<\/span><\/code><\/pre>\n<p><span class=\"code-copy button main small\" data-clipboard-text=\"bash languages.sh\">Copy<\/span><\/div>\n<p>The script will ask you to enter a country. For example, if you type \u201cLithuania\u201d it will match the first pattern and the\u00a0<code>echo<\/code>\u00a0command in that clause will be executed.<\/p>\n<p>The script will print the following output:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-output\" data-lang=\"output\">Enter the name of a country: Lithuania\r\nThe official language of Lithuania is Lithuanian<\/code><\/pre>\n<p><span class=\"code-copy button main small\" data-clipboard-text=\"Enter the name of a country: Lithuania\nThe official language of Lithuania is Lithuanian\">Copy<\/span><\/div>\n<p>If you enter a country that doesn\u2019t match any other pattern except the default wildcard asterisk symbol, let\u2019s say Argentina the script will execute\u00a0<code>echo<\/code>\u00a0command inside the default clause.<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-output\" data-lang=\"output\">Enter the name of a country: Argentina\r\nThe official language of Argentina is unknown<\/code><\/pre>\n<p><span class=\"code-copy button main small\" data-clipboard-text=\"Enter the name of a country: Argentina\nThe official language of Argentina is unknown\">Copy<\/span><\/div>\n<h2 id=\"conclusion\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/bash-case-statement\/#conclusion\" data-scroll=\"\">Conclusion<\/a><\/h2>\n<p>By now you should have a good understanding of how to write bash case statements. They are often used to pass parameters to a shell script from the command line. For example, the init scripts are using case statements for starting, stopping or restarting services.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=266594\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash case statements are generally used to simplify complex conditionals when you have multiple different choices. Using the case statement instead of nested if statements will help you make your bash scripts more readable and easier to maintain. The Bash case statement has a similar concept with the Javascript or C switch statement. The main &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/06\/bash-case-statement-linuxize\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash Case Statement | Linuxize&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10509","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/10509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=10509"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/10509\/revisions"}],"predecessor-version":[{"id":10520,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/10509\/revisions\/10520"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=10509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=10509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=10509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}