{"id":28284,"date":"2023-11-23T12:05:03","date_gmt":"2023-11-23T12:05:03","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=28284"},"modified":"2024-04-21T04:55:53","modified_gmt":"2024-04-21T04:55:53","slug":"bash-else-if","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/","title":{"rendered":"Else-If Statement in Bash"},"content":{"rendered":"<p style=\"text-align: justify;\">In the realm of programming, decision-making is a fundamental aspect of crafting effective code. Bash, a widely used Unix shell scripting language, provides a powerful tool for making decisions based on specific conditions: the <strong>else-if<\/strong> statement. This statement is used to handle <strong>multiple conditions<\/strong> in a loop that cannot be handled by the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/\" target=\"_blank\" rel=\"noopener\">if-else<\/a><\/u><\/strong> statement. So, if you want to run several conditions at a time in a bash script, using else-if is the way to go.<\/p>\n<p style=\"text-align: justify;\">In this article, I will provide you with a comprehensive overview of bash else-if conditional statements. I will also show you how to use the else-if statement in the bash script.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Practice_Files_bash-else-if.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files for Else-If Statement in Bash<\/span><\/a><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" 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: #999;color:#999\" 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><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#What_is_Else-If_Statement_in_Bash\" >What is Else-If Statement in Bash?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Syntax_of_Else-If_elif_Statement\" >Syntax of Else-If (elif) Statement<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#6_Examples_to_Use_Else-If_Statement_in_Bash\" >6 Examples to Use Else-If Statement in Bash<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#1_Check_if_Number_is_Positive_or_Negative\" >1. Check if Number is Positive or Negative<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#2_Check_if_File_is_Regular_File_or_Directory\" >2. Check if File is Regular File or Directory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#3_Verify_Username_Using_Elif\" >3. Verify Username Using Elif<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#4_Check_if_String_Contains_Specific_Numbered_Characters\" >4. Check if String Contains Specific Numbered Characters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#5_Check_Leap_Year_Using_Three_Conditions\" >5. Check Leap Year Using Three Conditions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#6_Check_Days_of_Week_With_Multiple_Conditions\" >6. Check Days of Week With Multiple Conditions<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#What_is_the_purpose_of_the_else-if_statement_in_Bash\" >What is the purpose of the else-if statement in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Are_else-if_and_elif_interchangeable\" >Are else-if and elif interchangeable?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#What_is_the_difference_between_an_else-if_statement_and_an_if-else_statement\" >What is the difference between an else-if statement and an if-else statement?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Can_I_have_multiple_elif_statements_in_a_single_if_block\" >Can I have multiple elif statements in a single if block?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Are_the_conditions_always_enclosed_in_square_brackets_%E2%80%9C_%E2%80%9D\" >Are the conditions always enclosed in square brackets \u201c[ ]\u201d?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Can_I_use_else-if_without_an_initial_if_statement\" >Can I use else-if without an initial if statement?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#Can_I_use_logical_operators_with_elif_conditions\" >Can I use logical operators with elif conditions?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Else-If_Statement_in_Bash\"><\/span>What is Else-If Statement in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In bash, the <strong>else-if<\/strong> (elif) is a <strong>decision-making<\/strong> statement that controls the execution of statements based on conditions that contain a boolean expression &#8220;true&#8221; or &#8220;false&#8221;. It works like if the first condition isn&#8217;t <strong>true<\/strong>, the second condition in the elif block is checked and executed, and when all the conditions are <strong>false<\/strong>, the else block is executed. You can have multiple elif conditions in one code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Syntax_of_Else-If_elif_Statement\"><\/span>Syntax of Else-If (elif) Statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here\u2019s the basic syntax of the <strong>elif<\/strong> statement:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">if [ condition1 ]; then\n\u00a0\u00a0\u00a0 # Code block executed if condition1 is true\nelif [ condition2 ]; then\n\u00a0\u00a0\u00a0 # Code block executed if condition2 is true\nelse\n\u00a0\u00a0\u00a0 # Code block executed if none of the conditions are true\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">If condition1 is <strong>true<\/strong>, then the script executes the if block. When condition2 is<strong> true<\/strong>, then it prints the elif block. If both condition1 and condition2 are <strong>false<\/strong>, then the else block is executed.<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"6_Examples_to_Use_Else-If_Statement_in_Bash\"><\/span>6 Examples to Use Else-If Statement in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Using the <strong>else-if<\/strong> (elif) conditional statement in bash scripting is a great way to handle many different conditions in a well-structured and easy-to-understand way. This makes your scripts more versatile and able to maintain different situations. Here are<strong> 6<\/strong> examples to use else-if in bash:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Check_if_Number_is_Positive_or_Negative\"><\/span>1. Check if Number is Positive or Negative<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To Check if a number is positive or negative, you can use an <code class=\"\" data-line=\"\">elif<\/code> statement. For that, the <code class=\"\" data-line=\"\">-gt<\/code> option is used inside the if block to indicate that it\u2019s a positive number when the input number is <strong>greater than<\/strong> zero. Conversely, the <code class=\"\" data-line=\"\">-lt<\/code> option inside the elif checks whether the number is <strong>less than<\/strong> zero indicating the number is negative. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter a number: &quot; num\n\nif [ &quot;$num&quot; -gt 0 ]; then\n\u00a0\u00a0\u00a0 echo &quot;The number is positive.&quot;\nelif [ &quot;$num&quot; -lt 0 ]; then\n\u00a0\u00a0\u00a0 echo &quot;The number is negative.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;The number is zero.&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">if<\/code> statement checks whether the number is <strong>greater<\/strong> than <strong>0<\/strong>. It executes &#8220;The number is positive&#8221; if the condition is true. But if it is false, the <code class=\"\" data-line=\"\">elif<\/code> statement checks whether the number is <strong>less<\/strong> than <strong>0<\/strong>, and prints &#8220;The number is negative&#8221;. If both conditions are false, the <code class=\"\" data-line=\"\">else<\/code> block is executed, printing &#8220;The number is zero.&#8221;.<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-28307\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1.checking-if-number-is-positive-or-negative.png\" alt=\"checking if a number is positive or negative using else if\" width=\"603\" height=\"225\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1.checking-if-number-is-positive-or-negative.png 603w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1.checking-if-number-is-positive-or-negative-300x112.png 300w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/>In this picture, you can see that number <strong>2<\/strong> is <strong>positive<\/strong> as it is greater than <strong>0<\/strong> and number<strong> -1<\/strong> is <strong>negative<\/strong> as it is less than zero.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Check_if_File_is_Regular_File_or_Directory\"><\/span>2. Check if File is Regular File or Directory<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To check whether a file is a <strong>regular file<\/strong> or <strong>directory<\/strong> or none of these, using a single code, use the <code class=\"\" data-line=\"\">-f<\/code> flag inside the <code class=\"\" data-line=\"\">if<\/code> condition to check if it is a regular file, and the <code class=\"\" data-line=\"\">-d<\/code> flag inside the <code class=\"\" data-line=\"\">elif<\/code> condition to check if the file is a directory. Let&#8217;s look at the following bash script to see how it works:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter a filename: &quot; filename\n\nif [ -f &quot;$filename&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s a regular file.&quot;\nelif [ -d &quot;$filename&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s a directory.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s neither a file nor a directory.&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">Here, the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/input\/read-input\/\" target=\"_blank\" rel=\"noopener\"><strong>read<\/strong><\/a> command is used to take user input. The <code class=\"\" data-line=\"\">-p<\/code> option displays a prompt, and the entered value is stored in the variable <strong>filename<\/strong>. Then the <code class=\"\" data-line=\"\">elif<\/code> statements check whether it&#8217;s a regular file or a directory. If neither condition is <strong>true<\/strong>, it outputs that <strong>it&#8217;s neither a file nor a directory<\/strong>.<\/div><\/div><img decoding=\"async\" class=\"aligncenter size-full wp-image-28308\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.checking-file-type.png\" alt=\"checking if a file is a regular file or directory using else if in bash\" width=\"622\" height=\"101\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.checking-file-type.png 622w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2.checking-file-type-300x49.png 300w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/>As you can see it shows that it\u2019s a directory because <strong>Downloads<\/strong> is a directory, not a regular file.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Verify_Username_Using_Elif\"><\/span>3. Verify Username Using Elif<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To verify the username using the <strong>elif<\/strong> statement, you can use the equal to <code class=\"\" data-line=\"\">==<\/code> operator within the condition block. If the user name is equal to the right user name, only then you will get access. As an elif structure let&#8217;s check multiple conditions, you can even check multiple users at the same time. Here is a bash script to check for <strong>2<\/strong> usernames:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter your username: &quot; username\n\nif [ &quot;$username&quot; == &quot;mou&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;Welcome, mou!&quot;\nelif [ &quot;$username&quot; == &quot;john&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;Welcome, john!&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;Invalid username.&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">Here, the <strong>if statement<\/strong> checks if the entered username is equal to &#8220;mou.&#8221; and prints\u00a0 &#8220;Welcome, mou!&#8221; if it is <strong>true<\/strong>. The <strong>else-if<\/strong> statement checks if the entered username is equal to &#8220;john&#8221; and prints &#8220;Welcome, john!&#8221; if it is true. If the entered username is not &#8220;mou&#8221; or &#8220;john,&#8221; it prints an &#8220;Invalid username&#8221; message.<\/div><\/div><img decoding=\"async\" class=\"aligncenter size-full wp-image-28309\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.verifying-username.png\" alt=\"verifying username with else if statement in bash\" width=\"698\" height=\"292\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.verifying-username.png 698w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3.verifying-username-300x126.png 300w\" sizes=\"(max-width: 698px) 100vw, 698px\" \/><\/p>\n<p style=\"text-align: justify;\">When I signed in with the username \u201cmou\u201d and \u201cjohn\u201d, I got the welcome message printed out. But when I signed in with another username \u201cmitu\u201d, it showed me an \u201cinvalid username\u201d as it is not a valid username on my system.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Check_if_String_Contains_Specific_Numbered_Characters\"><\/span>4. Check if String Contains Specific Numbered Characters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To check if a string contains specific numbered characters in bash, use <code class=\"\" data-line=\"\">&quot;${#string}&quot;<\/code> syntax inside the <code class=\"\" data-line=\"\">else-if<\/code> conditions which check the number of characters present in the strings including spaces. Here, the <code class=\"\" data-line=\"\">-eq<\/code> option verifies whether the string has <strong>3 characters<\/strong>, <strong>5 characters<\/strong>, or a <strong>different number<\/strong> of characters. Let\u2019s see the detailed bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter a string: &quot; string\n\nif [ &quot;${#string}&quot; -eq 3 ]; then\n\u00a0\u00a0\u00a0 echo &quot;The string has three characters.&quot;\nelif [ &quot;${#string}&quot; -eq 5 ]; then\n\u00a0\u00a0\u00a0 echo &quot;The string has five characters.&quot;\nelse\n\u00a0\u00a0 \u00a0echo &quot;The string has a different number of characters.&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">In this script, the<strong> if<\/strong> condition checks whether the length of the string is equal to <strong>3<\/strong>. If so, it executes &#8220;The string has three characters.&#8221;. The <strong>elif<\/strong> condition checks if the string length is equal to <strong>5<\/strong>. It executes &#8220;The string has five characters.&#8221; if the condition is true. Otherwise, it prints the <strong>else block<\/strong> message &#8220;The string has a different number of characters.&#8221;.<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28310\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.checking-string-length.png\" alt=\"checking if a string contains specific characters using else if\" width=\"700\" height=\"252\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.checking-string-length.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4.checking-string-length-300x108.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">In this image, we can see that when the string &#8220;hello&#8221; is entered, it indicates that the string contains <strong>5 characters<\/strong>. However, when I entered the string &#8220;hello world&#8221; (which does not contain 3 or 5 characters), it showed that the string contains <strong>different characters<\/strong>. Moreover, when I entered &#8220;mou&#8221;, which is a string with<strong> 3 characters<\/strong>, it showed that &#8220;it has 3 characters&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Check_Leap_Year_Using_Three_Conditions\"><\/span>5. Check Leap Year Using Three Conditions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can use more than two conditions with an else-if structure. In this example, I will show you how to check whether a particular year is a leap year.<\/p>\n<p style=\"text-align: justify;\">Basically, a leap year is a year that has <strong>366 days<\/strong> instead of the usual <strong>365 days<\/strong>. There are <strong>three conditions<\/strong> that must be met for a year to be a leap year: the year must be divisible by 4, the year must not be divisible by 100, and the year must be divisible by 400. Here, the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/modulo-if\/#What_is_Modulo_in_Bash\" target=\"_blank\" rel=\"noopener\"><strong><u>modulo<\/u><\/strong><\/a>\u00a0<code class=\"\" data-line=\"\">(%)<\/code> is used to calculate the remainder of the divisions. Let\u2019s see a full bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter a year: &quot; year\n\nif [ &quot;$((year % 400))&quot; -eq 0 ]; then\n\u00a0\u00a0\u00a0 echo &quot;$year is a leap year.&quot;\nelif [ &quot;$((year % 100))&quot; -eq 0 ]; then\n\u00a0\u00a0\u00a0 echo &quot;$year is not a leap year.&quot;\nelif [ &quot;$((year % 4))&quot; -eq 0 ]; then\n\u00a0\u00a0\u00a0 echo &quot;$year is a leap year.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;$year is not a leap year.&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <strong>first if<\/strong> statement checks if the year is divisible by <strong>400<\/strong>. If it is, then the year is a leap year and the script prints a message to the console. The <strong>second elif <\/strong>statement checks if the year is divisible by <strong>100<\/strong>. If so, then the script prints &#8220;$year is not a leap year.&#8221;. The <strong>third elif<\/strong> statement checks if the year is divisible by <strong>4<\/strong>. If it is, then the year is a leap year and the script executes the corresponding code block. The <strong>else block<\/strong> is used if none of the conditions are <strong>true<\/strong>. In this case, the script prints &#8220;$year is not a leap year.&#8221;.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28311\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5.checking-leap-year.png\" alt=\"checking if a year is leap year using else if statement\" width=\"621\" height=\"147\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5.checking-leap-year.png 621w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5.checking-leap-year-300x71.png 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/p>\n<p style=\"text-align: justify;\">Since the year 2023 is not divisible by <strong>400, 100<\/strong>, and <strong>4,<\/strong> it displays else block code &#8220;2023 is not a leap year&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Check_Days_of_Week_With_Multiple_Conditions\"><\/span>6. Check Days of Week With Multiple Conditions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">If you require to execute <strong>multiple conditions<\/strong> within a loop, you can use the <code class=\"\" data-line=\"\">elif<\/code> statement. Here\u2019s a simple example to check a day of the week using elif expression. The <code class=\"\" data-line=\"\">-eq<\/code> option is used here to check the equality between variables:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter a day (1-7): &quot; day\n\nif [ &quot;$day&quot; -eq 1 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Monday.&quot;\nelif [ &quot;$day&quot; -eq 2 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Tuesday.&quot;\nelif [ &quot;$day&quot; -eq 3 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Wednesday.&quot;\nelif [ &quot;$day&quot; -eq 4 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Thursday.&quot;\nelif [ &quot;$day&quot; -eq 5 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Friday.&quot;\nelif [ &quot;$day&quot; -eq 6 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Saturday.&quot;\nelif [ &quot;$day&quot; -eq 7 ]; then\n\u00a0\u00a0\u00a0 echo &quot;It&#039;s Sunday.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;Invalid day.&quot;\nfi<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">-eq<\/code> operator checks whether the input value is equal to 1, then it prints it\u2019s a Monday which is the<strong> first day<\/strong> of the week. Similarly, if you enter value 2, it will print it\u2019s a Tuesday which is the <strong>second day<\/strong> of the week. Similarly, it prints the other days of the week. However, if you put any number except 1 to 7 then it executes &#8220;Invalid day.&#8221;.<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28312\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6.checking-a-day-of-the-week.png\" alt=\"checking a day of the week using else if\" width=\"548\" height=\"292\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6.checking-a-day-of-the-week.png 548w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6.checking-a-day-of-the-week-300x160.png 300w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/p>\n<p style=\"text-align: justify;\">After entering the number <strong>7<\/strong>, it will display that it is Sunday, which is the <strong>seventh day<\/strong> of the week. If you enter the number <strong>1<\/strong>, it will execute Monday (the <strong>first day<\/strong> of the week). However, if you enter the number <strong>8<\/strong>, which does not represent any day in the week, it will print &#8221; Invalid day&#8221;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, I have presented an in-depth analysis of the <strong>&#8216;else-if&#8217;<\/strong> conditional statement in bash. By now, you are aware of the concept of &#8216;else-if&#8217; statements in bash and the significance of using them in bash scripts. Furthermore, I have presented <strong>6<\/strong> practical &#8216;else-if&#8217; statements to help you gain a better understanding of the concept. Thank you for reading.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_purpose_of_the_else-if_statement_in_Bash\"><\/span>What is the purpose of the else-if statement in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>else-if<\/strong> statement, often written as elif in Bash allows you to <strong>test multiple conditions sequentially<\/strong>. It provides an alternative block of code to be executed if the preceding if statement evaluates to <strong>false<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Are_else-if_and_elif_interchangeable\"><\/span>Are else-if and elif interchangeable?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Yes, <strong>else-if<\/strong> and <strong>elif<\/strong> are interchangeable in Bash. Both keywords serve the same purpose in creating conditional branches.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_an_else-if_statement_and_an_if-else_statement\"><\/span>What is the difference between an else-if statement and an if-else statement?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">An else-if statement allows you to evaluate <strong>multiple conditions<\/strong> sequentially, while an if-else statement only allows you to evaluate <strong>one condition<\/strong>. An else-if statement is more <strong>versatile<\/strong> than an if-else statement because it allows you to handle more complex decision-making scenarios.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_have_multiple_elif_statements_in_a_single_if_block\"><\/span>Can I have multiple elif statements in a single if block?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Yes, definitely. You can have <strong>multiple elif<\/strong> statements to test additional conditions. The code inside the first true condition or the else block will be executed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Are_the_conditions_always_enclosed_in_square_brackets_%E2%80%9C_%E2%80%9D\"><\/span>Are the conditions always enclosed in square brackets \u201c[ ]\u201d?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, in Bash, the conditions must be enclosed in <strong>square brackets <\/strong>\u201c[]\u201d. The spaces around the square brackets are crucial for proper syntax.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_use_else-if_without_an_initial_if_statement\"><\/span>Can I use else-if without an initial if statement?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">No, <strong>else-if<\/strong> (elif) is always used in conjunction with an initial <strong>if <\/strong>statement. It follows the initial if block to test additional conditions if the initial condition is false.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_use_logical_operators_with_elif_conditions\"><\/span>Can I use logical operators with elif conditions?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, you can use logical operators like <code class=\"\" data-line=\"\">-a<\/code> (and) and <code class=\"\" data-line=\"\">-o<\/code> (or) to combine multiple conditions within the <strong>square brackets<\/strong>.<\/p>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/\" target=\"_blank\" rel=\"noopener\">If Statement in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/\" target=\"_blank\" rel=\"noopener\">If Else in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/case\/\" target=\"_blank\" rel=\"noopener\">Case Statement in Bash<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to\u00a0<span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Conditional Statements&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/\" target=\"_blank\" rel=\"noopener\">Bash Conditional Statements<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/strong><\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"What is the purpose of the else-if statement in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The else-if statement, often written as elif in Bash, allows you to test multiple conditions in a sequential manner. It provides an alternative block of code to be executed if the preceding if statement evaluates to false.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Are else-if and elif interchangeable?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, else-if and elif are interchangeable in Bash. Both keywords serve the same purpose in creating conditional branches.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the difference between an else-if statement and an if-else statement?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"An else-if statement allows you to evaluate multiple conditions sequentially, while an if-else statement only allows you to evaluate one condition. An else-if statement is more versatile than an if-else statement because it allows you to handle more complex decision-making scenarios.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Can I have multiple elif statements in a single if block?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, definitely. You can have multiple elif statements to test additional conditions. The code inside the first true condition or the else block will be executed.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Are the conditions always enclosed in square brackets \u201c[ ]\u201d?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, in Bash, the conditions must be enclosed in square brackets \u201c[]\u201d. The spaces around the square brackets are crucial for proper syntax.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Can I use else-if without an initial if statement?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"No, else-if (elif) is always used in conjunction with an initial if statement. It follows the initial if block to test additional conditions if the initial condition is false.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Can I use logical operators with elif conditions?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, you can use logical operators like -a (and) and -o (or) to combine multiple conditions within the square brackets.\"\n    }\n  }]\n}\n<\/script><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;28284&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;6&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (6 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Else-If Statement in Bash&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            5\/5 - (6 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of programming, decision-making is a fundamental aspect of crafting effective code. Bash, a widely used Unix shell &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Else-If Statement in Bash\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#more-28284\" aria-label=\"Read more about Else-If Statement in Bash\">Read more<\/a><\/p>\n","protected":false},"author":314908,"featured_media":28313,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[165],"class_list":["post-28284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-conditional-statements","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Else-If Statement in Bash - LinuxSimply<\/title>\n<meta name=\"description\" content=\"In this article, I have described the basics of else-if statements. Additionally, I have shown 6 examples of using else if in bash.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Else-If Statement in Bash - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"In this article, I have described the basics of else-if statements. Additionally, I have shown 6 examples of using else if in bash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/mitalimostakim.mou\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-23T12:05:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-21T04:55:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Bash-else-if.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mitu Akter Mou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mitu Akter Mou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/\"},\"author\":{\"name\":\"Mitu Akter Mou\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\"},\"headline\":\"Else-If Statement in Bash\",\"datePublished\":\"2023-11-23T12:05:03+00:00\",\"dateModified\":\"2024-04-21T04:55:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/\"},\"wordCount\":1801,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Bash-else-if.png\",\"keywords\":[\"conditional statements\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/\",\"name\":\"Else-If Statement in Bash - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Bash-else-if.png\",\"datePublished\":\"2023-11-23T12:05:03+00:00\",\"dateModified\":\"2024-04-21T04:55:53+00:00\",\"description\":\"In this article, I have described the basics of else-if statements. Additionally, I have shown 6 examples of using else if in bash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Bash-else-if.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Bash-else-if.png\",\"width\":400,\"height\":400,\"caption\":\"bash else if\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/else-if\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash Conditional Statements\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Else-If Statement in Bash\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\",\"name\":\"Mitu Akter Mou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"caption\":\"Mitu Akter Mou\"},\"description\":\"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/mitalimostakim.mou\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/mitu-akter-mou-915537198\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/mou\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Else-If Statement in Bash - LinuxSimply","description":"In this article, I have described the basics of else-if statements. Additionally, I have shown 6 examples of using else if in bash.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/","og_locale":"en_US","og_type":"article","og_title":"Else-If Statement in Bash - LinuxSimply","og_description":"In this article, I have described the basics of else-if statements. Additionally, I have shown 6 examples of using else if in bash.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/mitalimostakim.mou\/","article_published_time":"2023-11-23T12:05:03+00:00","article_modified_time":"2024-04-21T04:55:53+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Bash-else-if.png","type":"image\/png"}],"author":"Mitu Akter Mou","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mitu Akter Mou","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/"},"author":{"name":"Mitu Akter Mou","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689"},"headline":"Else-If Statement in Bash","datePublished":"2023-11-23T12:05:03+00:00","dateModified":"2024-04-21T04:55:53+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/"},"wordCount":1801,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Bash-else-if.png","keywords":["conditional statements"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/","name":"Else-If Statement in Bash - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Bash-else-if.png","datePublished":"2023-11-23T12:05:03+00:00","dateModified":"2024-04-21T04:55:53+00:00","description":"In this article, I have described the basics of else-if statements. Additionally, I have shown 6 examples of using else if in bash.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Bash-else-if.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Bash-else-if.png","width":400,"height":400,"caption":"bash else if"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/else-if\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash Conditional Statements","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/"},{"@type":"ListItem","position":4,"name":"Else-If Statement in Bash"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689","name":"Mitu Akter Mou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","caption":"Mitu Akter Mou"},"description":"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/mitalimostakim.mou\/","https:\/\/www.linkedin.com\/in\/mitu-akter-mou-915537198\/"],"url":"https:\/\/linuxsimply.com\/author\/mou\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/28284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314908"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=28284"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/28284\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/28313"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=28284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=28284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=28284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}