{"id":2390,"date":"2025-11-03T15:43:13","date_gmt":"2025-11-03T15:43:13","guid":{"rendered":"https:\/\/www.ucartz.com\/blog\/?p=2390"},"modified":"2025-11-03T15:49:55","modified_gmt":"2025-11-03T15:49:55","slug":"understanding-bash-ifelse-statements","status":"publish","type":"post","link":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/","title":{"rendered":"Understanding Bash If\u2026Else Statements"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Conditional statements are a core concept in any programming language, and Bash is no exception. In shell scripting, decision-making lets you control the flow of execution based on whether a condition is true or false. Bash provides several options for this: <strong>if<\/strong>, <strong>if\u2026else<\/strong>, <strong>if\u2026elif\u2026else<\/strong>, and <strong>nested if<\/strong> statements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, we\u2019ll walk through the basics of the <strong>Bash if\u2026else statement<\/strong>, explain how it works, and demonstrate its usage with practical examples.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>The Basic If Statement<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">There are different forms of Bash if conditional statements. The simplest form of conditional execution in Bash looks like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>if TEST-COMMAND<br>then<br>\u00a0\u00a0STATEMENTS<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The if statement in Bash begins with the <strong>if<\/strong> keyword, followed by a <strong>conditional expression<\/strong>, and then the <strong>then<\/strong> keyword. The statement is closed using the <strong>fi<\/strong> keyword (which is simply <em>\u201cif\u201d<\/em> spelled backward).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>if [ TEST-COMMAND ]; then<br>\u00a0\u00a0\u00a0\u00a0STATEMENTS<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the <strong>TEST-COMMAND<\/strong> evaluates to <strong>True<\/strong>, the <strong>STATEMENTS<\/strong> inside the block are executed. If the <strong>TEST-COMMAND<\/strong> returns <strong>False<\/strong>, nothing happens \u2014 the commands inside the block are skipped.<br><br><strong>Example: Check if a Number is Greater than 10<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>#!\/bin\/bash<br>echo -n \"Enter a number: \"<br>read VAR<br>if [[ $VAR -gt 10 ]]<br>then<br>\u00a0\u00a0echo \"The variable is greater than 10.\"<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You need to save the code in a file and have to run it from the command line. For example, if you save the code in a file as: bash samplecode.sh and when executed, the script will ask to enter a number. You need to enter 15, the output will be:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The variable is greater than 10.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>If\u2026Else Statement<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The following is the form of the Bash if&#8230;else statement:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>if TEST-COMMAND<br>then<br>\u00a0\u00a0STATEMENTS1<br>else<br>\u00a0STATEMENTS2<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the TEST-COMMAND evaluates to True, the STATEMENTS1 will be executed. Otherwise, if TEST-COMMAND returns False, the STATEMENTS2 will be executed. You can have only one else clause in the statement.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\"><code>#!\/bin\/bash<br>echo -n \"Enter a number: \"<br>read VAR<br>if [[ $VAR -gt 10 ]]<br>then<br>\u00a0\u00a0echo \"The variable is greater than 10.\"<br>else<br>\u00a0\u00a0echo \"The variable is equal to or less than 10.\"<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you run the code above, it prompts you to enter a number. Now, the script will display an output based on whether the number is greater than, less than, or equal to 10.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>If\u2026Elif\u2026Else Statement<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When you need to check multiple conditions, you can use elif. The following is the form of the Bash if&#8230;elif&#8230;else statement:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>if TEST-COMMAND1<br>then<br>\u00a0\u00a0STATEMENTS1<br>elif TEST-COMMAND2<br>then<br>\u00a0\u00a0STATEMENTS2<br>else<br>\u00a0\u00a0STATEMENTS3<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If <strong>TEST-COMMAND1<\/strong> evaluates to <strong>True<\/strong>, the <strong>STATEMENTS1<\/strong> block is executed. (OR)<br>If <strong>TEST-COMMAND2<\/strong> evaluates to <strong>True<\/strong>, then <strong>STATEMENTS2<\/strong> will be executed instead.<br>If none of the test commands evaluate to <strong>True<\/strong>, the <strong>STATEMENTS3<\/strong> block (inside the else clause) will run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can include one or more <strong>elif<\/strong> clauses between the if and else sections, depending on how many conditions you want to check. The <strong>else<\/strong> clause itself is optional.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each condition is evaluated <strong>in order<\/strong>, from top to bottom. As soon as one condition evaluates to <strong>True<\/strong>, the remaining conditions are skipped, and the script continues executing after the entire if block.<br><br><strong>Example:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>#!\/bin\/bash<br>echo -n \"Enter a number: \"<br>read VAR<br>if [[ $VAR -gt 10 ]]<br>then<br>\u00a0\u00a0echo \"The variable is greater than 10.\"<br>elif [[ $VAR -eq 10 ]]<br>then<br>\u00a0\u00a0echo \"The variable is equal to 10.\"<br>else<br>\u00a0\u00a0echo \"The variable is less than 10.\"<br>fi<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Nested If Statements<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Bash also allows you to place if statements inside other if statements. While powerful, they can quickly become messy, so use them carefully.<br><br>Below is a script that will ask you to enter three numbers and, based on the input it print the largest number among those three numbers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example: Find the Largest of Three Numbers<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>#!\/bin\/bash<br>echo -n \"Enter the first number: \"<br>read VAR1<br>echo -n \"Enter the second number: \"<br>read VAR2<br>echo -n \"Enter the third number: \"<br>read VAR3<br>if [[ $VAR1 -ge $VAR2 ]]<br>then<br>\u00a0\u00a0if [[ $VAR1 -ge $VAR3 ]]<br>\u00a0\u00a0then<br>\u00a0\u00a0\u00a0\u00a0echo \"$VAR1 is the largest number.\"<br>\u00a0\u00a0else<br>\u00a0\u00a0\u00a0\u00a0echo \"$VAR3 is the largest number.\"<br>\u00a0\u00a0fi<br>else<br>\u00a0\u00a0if [[ $VAR2 -ge $VAR3 ]]<br>\u00a0\u00a0then<br>\u00a0\u00a0\u00a0\u00a0echo \"$VAR2 is the largest number.\"<br>\u00a0\u00a0else<br>\u00a0\u00a0\u00a0\u00a0echo \"$VAR3 is the largest number.\"<br>\u00a0\u00a0fi<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output:<\/strong><br>Once the above code is executed, the output will look like:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enter the first number: 8<br>Enter the second number: 4<br>Enter the third number: 1<br><br><strong>8 is the largest number.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Tip:<\/strong> For complex conditions, consider using the case statement instead\u2014it is recommended.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using Multiple Conditions<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>logical OR <\/strong>and <strong>logical AND<\/strong> operators enable you to combine multiple conditions within an if statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can combine conditions using <strong>logical operators<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>&amp;&amp; (AND):<\/strong> all conditions must be true.<\/li>\n\n\n\n<li><strong>|| (OR):<\/strong> at least one condition must be true.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an updated version of the script that determines the largest number among three values. In this approach, instead of using nested if statements, we utilize the <strong>logical AND (&amp;&amp;)<\/strong> operator to make the code more concise and easier to read.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example with AND Operator:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>#!\/bin\/bash<br>echo -n \"Enter the first number: \"<br>read VAR1<br>echo -n \"Enter the second number: \"<br>read VAR2<br>echo -n \"Enter the third number: \"<br>read VAR3<br>if [[ $VAR1 -ge $VAR2 ]] &amp;&amp; [[ $VAR1 -ge $VAR3 ]]<br>then<br>\u00a0\u00a0echo \"$VAR1 is the largest number.\"<br>elif [[ $VAR2 -ge $VAR1 ]] &amp;&amp; [[ $VAR2 -ge $VAR3 ]]<br>then<br>\u00a0\u00a0echo \"$VAR2 is the largest number.\"<br>else<br>\u00a0\u00a0echo \"$VAR3 is the largest number.\"<br>fi<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output:<\/strong><br>Once the above code is executed, the output will look like:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enter the first number: 8<br>Enter the second number: 4<br>Enter the third number: 1<br><br><strong>8 is the largest number.<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Test Operators in Bash<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In Bash, the test command can be written in any of the following forms:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>test EXPRESSION<br>[ EXPRESSION ]<br>[[ EXPRESSION ]]<\/code><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure your script is portable, it\u2019s best to use the traditional test or<strong> [ &#8230; ] syntax<\/strong>, as these are supported by all <strong>POSIX-compliant<\/strong> shells.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The newer<strong> [[ &#8230; ]]<\/strong> syntax is an enhanced version of test, offering additional features and improved syntax handling. It is supported by most modern shells such as Bash, Zsh, and Ksh.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To negate a test expression, use the <strong>logical NOT (!)<\/strong> operator. When comparing strings, always enclose them in <strong>single or double quotes<\/strong> to prevent issues with word splitting or globbing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The test command (or its shorthand<strong> [ ] <\/strong>and <strong>[[ ]]<\/strong>) evaluates conditions. Here are some common operators:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>String Operators<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>-n VAR<\/strong> \u2192 Returns <strong>true<\/strong> if the variable <strong>VAR<\/strong> is <strong>not empty<\/strong><\/li>\n\n\n\n<li><strong>-z VAR<\/strong><strong> <\/strong>\u2192 Returns <strong>true<\/strong> if the variable <strong>VAR<\/strong> is <strong>empty<\/strong><\/li>\n\n\n\n<li><strong>STRING1 = STRING2<\/strong> \u2192 Returns <strong>true<\/strong> if both strings are <strong>equal<\/strong><\/li>\n\n\n\n<li><strong>STRING1 != STRING2<\/strong> \u2192 Returns <strong>true<\/strong> if the strings are <strong>different<\/strong><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Integer Operators<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>-eq <\/strong>\u2192 Equal to<\/li>\n\n\n\n<li><strong>-ne <\/strong>\u2192 Not equal to<\/li>\n\n\n\n<li><strong>-gt<\/strong> \u2192 Greater than<\/li>\n\n\n\n<li><strong>-lt <\/strong>\u2192 Less than<\/li>\n\n\n\n<li><strong>-ge<\/strong> \u2192 Greater than or equal to<\/li>\n\n\n\n<li><strong>-le<\/strong> \u2192\u00a0 Less than or equal to<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>File Test Operators<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>-e FILE<\/strong> \u2192 Returns <strong>true<\/strong> if the file <strong>exists<\/strong><\/li>\n\n\n\n<li><strong>-f FILE <\/strong>\u2192 Returns <strong>true<\/strong> if the file <strong>exists<\/strong> and is a <strong>regular file<\/strong><\/li>\n\n\n\n<li><strong>-d FILE<\/strong> \u2192 Returns <strong>true<\/strong> if the file <strong>exists<\/strong> and is a <strong>directory<\/strong><\/li>\n\n\n\n<li><strong>-r FILE<\/strong> \u2192 Returns <strong>true<\/strong> if the file is <strong>readable<\/strong><\/li>\n\n\n\n<li><strong>-w FILE <\/strong>\u2192 Returns <strong>true<\/strong> if the file is <strong>writable<\/strong><\/li>\n\n\n\n<li><strong>-x FILE <\/strong>\u2192 Returns <strong>true<\/strong> if the file is <strong>executable<\/strong><\/li>\n\n\n\n<li><strong>-h FILE<\/strong> \u2192 Returns <strong>true<\/strong> if the file is a <strong>symbolic link<\/strong><br><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Integer Comparison Operators<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>INTEGER1 -eq INTEGER2<\/strong>\u2192 Returns true if <strong>INTEGER<\/strong>1 is equal to <strong>INTEGER2<\/strong><\/li>\n\n\n\n<li><strong>INTEGER1 -gt INTEGER2<\/strong>\u2192 Returns true if <strong>INTEGER1<\/strong> is greater than<strong> INTEGER2<\/strong><\/li>\n\n\n\n<li><strong>INTEGER1 -lt INTEGER2<\/strong>\u2192 Returns true if <strong>INTEGER1 <\/strong>is less than<strong> INTEGER2<\/strong><\/li>\n\n\n\n<li><strong>INTEGER1 -ge INTEGER2<\/strong>\u2192 Returns true if <strong>INTEGER1<\/strong> is greater than or equal to <strong>INTEGER2<\/strong><\/li>\n\n\n\n<li><strong>INTEGER1 -le INTEGER2<\/strong>\u2192 Returns true if <strong>INTEGER<\/strong>1 is less than or equal to <strong>INTEGER2<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important: <\/strong>Always quote strings in comparisons to prevent errors from word splitting or globbing.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>if statement<\/strong> in Bash is a powerful way to add logic to your shell scripts. The <strong>if<\/strong><strong>, <\/strong><strong>if&#8230;else<\/strong><strong>, and <\/strong><strong>if&#8230;elif&#8230;else<\/strong> statements in Bash allow you to control the flow of your script by evaluating specific conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a condition evaluates to <strong>false<\/strong>, the commands in the optional <strong>else<\/strong> clause will execute instead. This makes your scripts more powerful, dynamic, and adaptable to real-world tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By mastering these conditionals, you can create smarter, more efficient Bash scripts that respond dynamically to user input and system states.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conditional statements are a core concept in any programming language, and Bash is no exception. In shell scripting, decision-making lets you control the flow of execution based on whether a condition is true or false. Bash provides several options for this: if, if\u2026else, if\u2026elif\u2026else, and nested if statements. In this guide, we\u2019ll walk through the [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":2391,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[394],"class_list":["post-2390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-tutorials-guides","tag-linux"],"blocksy_meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bash If Else Statement Explained with Examples<\/title>\n<meta name=\"description\" content=\"Learn how to use the Bash if-else statement to make decisions in shell scripts using if, elif, and nested conditionals 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:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bash If Else Statement Explained with Examples\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the Bash if-else statement to make decisions in shell scripts using if, elif, and nested conditionals in Bash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting and IT Consultancy Services\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-03T15:43:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-03T15:49:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2025\/11\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Anita Sreelal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anita Sreelal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/\"},\"author\":{\"name\":\"Anita Sreelal\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#\\\/schema\\\/person\\\/84e61e902dda958ac7d50776983f6f15\"},\"headline\":\"Understanding Bash If\u2026Else Statements\",\"datePublished\":\"2025-11-03T15:43:13+00:00\",\"dateModified\":\"2025-11-03T15:49:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/\"},\"wordCount\":1084,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg\",\"keywords\":[\"Linux\"],\"articleSection\":[\"Linux Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/\",\"url\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/\",\"name\":\"Bash If Else Statement Explained with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg\",\"datePublished\":\"2025-11-03T15:43:13+00:00\",\"dateModified\":\"2025-11-03T15:49:55+00:00\",\"description\":\"Learn how to use the Bash if-else statement to make decisions in shell scripts using if, elif, and nested conditionals in Bash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg\",\"contentUrl\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg\",\"width\":1920,\"height\":1080,\"caption\":\"Bash if elif else syntax\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/understanding-bash-ifelse-statements\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding Bash If\u2026Else Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/\",\"name\":\"Web Hosting and IT Consultancy Services\",\"description\":\"Discover the Potential of Digital Transformation through Effortless Hosting and Professional IT Consulting!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#organization\",\"name\":\"Web Hosting and IT Consultancy Services\",\"url\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/ucartzLogo-1.png\",\"contentUrl\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/ucartzLogo-1.png\",\"width\":165,\"height\":50,\"caption\":\"Web Hosting and IT Consultancy Services\"},\"image\":{\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/#\\\/schema\\\/person\\\/84e61e902dda958ac7d50776983f6f15\",\"name\":\"Anita Sreelal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f4b69147302b926e61568e95a5bb0e1ce6d6f81be49bedc8b561522e8b82286b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f4b69147302b926e61568e95a5bb0e1ce6d6f81be49bedc8b561522e8b82286b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f4b69147302b926e61568e95a5bb0e1ce6d6f81be49bedc8b561522e8b82286b?s=96&d=mm&r=g\",\"caption\":\"Anita Sreelal\"},\"sameAs\":[\"https:\\\/\\\/www.ucartz.com\"],\"url\":\"https:\\\/\\\/www.ucartz.com\\\/blog\\\/author\\\/anita-sreelal\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bash If Else Statement Explained with Examples","description":"Learn how to use the Bash if-else statement to make decisions in shell scripts using if, elif, and nested conditionals 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:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/","og_locale":"en_US","og_type":"article","og_title":"Bash If Else Statement Explained with Examples","og_description":"Learn how to use the Bash if-else statement to make decisions in shell scripts using if, elif, and nested conditionals in Bash.","og_url":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/","og_site_name":"Web Hosting and IT Consultancy Services","article_published_time":"2025-11-03T15:43:13+00:00","article_modified_time":"2025-11-03T15:49:55+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2025\/11\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg","type":"image\/jpeg"}],"author":"Anita Sreelal","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Anita Sreelal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#article","isPartOf":{"@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/"},"author":{"name":"Anita Sreelal","@id":"https:\/\/www.ucartz.com\/blog\/#\/schema\/person\/84e61e902dda958ac7d50776983f6f15"},"headline":"Understanding Bash If\u2026Else Statements","datePublished":"2025-11-03T15:43:13+00:00","dateModified":"2025-11-03T15:49:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/"},"wordCount":1084,"commentCount":0,"publisher":{"@id":"https:\/\/www.ucartz.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2025\/11\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg","keywords":["Linux"],"articleSection":["Linux Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/","url":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/","name":"Bash If Else Statement Explained with Examples","isPartOf":{"@id":"https:\/\/www.ucartz.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#primaryimage"},"image":{"@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2025\/11\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg","datePublished":"2025-11-03T15:43:13+00:00","dateModified":"2025-11-03T15:49:55+00:00","description":"Learn how to use the Bash if-else statement to make decisions in shell scripts using if, elif, and nested conditionals in Bash.","breadcrumb":{"@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#primaryimage","url":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2025\/11\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg","contentUrl":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2025\/11\/Black-and-Teal-Modern-Web-Developer-Presentation.jpg","width":1920,"height":1080,"caption":"Bash if elif else syntax"},{"@type":"BreadcrumbList","@id":"https:\/\/www.ucartz.com\/blog\/understanding-bash-ifelse-statements\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ucartz.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Understanding Bash If\u2026Else Statements"}]},{"@type":"WebSite","@id":"https:\/\/www.ucartz.com\/blog\/#website","url":"https:\/\/www.ucartz.com\/blog\/","name":"Web Hosting and IT Consultancy Services","description":"Discover the Potential of Digital Transformation through Effortless Hosting and Professional IT Consulting!","publisher":{"@id":"https:\/\/www.ucartz.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ucartz.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.ucartz.com\/blog\/#organization","name":"Web Hosting and IT Consultancy Services","url":"https:\/\/www.ucartz.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ucartz.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2021\/08\/ucartzLogo-1.png","contentUrl":"https:\/\/www.ucartz.com\/blog\/wp-content\/uploads\/2021\/08\/ucartzLogo-1.png","width":165,"height":50,"caption":"Web Hosting and IT Consultancy Services"},"image":{"@id":"https:\/\/www.ucartz.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.ucartz.com\/blog\/#\/schema\/person\/84e61e902dda958ac7d50776983f6f15","name":"Anita Sreelal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f4b69147302b926e61568e95a5bb0e1ce6d6f81be49bedc8b561522e8b82286b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f4b69147302b926e61568e95a5bb0e1ce6d6f81be49bedc8b561522e8b82286b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f4b69147302b926e61568e95a5bb0e1ce6d6f81be49bedc8b561522e8b82286b?s=96&d=mm&r=g","caption":"Anita Sreelal"},"sameAs":["https:\/\/www.ucartz.com"],"url":"https:\/\/www.ucartz.com\/blog\/author\/anita-sreelal\/"}]}},"_links":{"self":[{"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/posts\/2390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/comments?post=2390"}],"version-history":[{"count":3,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/posts\/2390\/revisions"}],"predecessor-version":[{"id":2397,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/posts\/2390\/revisions\/2397"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/media\/2391"}],"wp:attachment":[{"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/media?parent=2390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/categories?post=2390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ucartz.com\/blog\/wp-json\/wp\/v2\/tags?post=2390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}