{"id":2965,"date":"2023-08-11T14:43:41","date_gmt":"2023-08-11T09:13:41","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2965"},"modified":"2023-08-11T14:43:44","modified_gmt":"2023-08-11T09:13:44","slug":"how-to-use-conditional-statements-in-bash-the-if-else-fi-basics","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/","title":{"rendered":"How to Use Conditional Statements in Bash: The If-Else Fi Basics"},"content":{"rendered":"\n<p>Conditional Statements in bash help you make decision. For example, you can tell your script to run this code if certain conditions are met and do that if some other conditions are met. It is one of the most basic concept in bash (and in programming).<\/p>\n\n\n\n<p>In this article, I will talk about basics of How to use Conditional Statements in Bash &#8211; if, if-else, if-elif-else, case, and various other types of conditional expressions like <code>-gt<\/code>, <code>-lt<\/code>, <code>&lt;<\/code>, <code>><\/code>, <code>-nt<\/code> (file1 is newer than file2), etc.<\/p>\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 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 \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#The_if_Statement\" title=\"The if Statement\">The if Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#The_if-else_Statement\" title=\"The if-else Statement\">The if-else Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#The_if-elif-else_Statement\" title=\"The if-elif-else Statement\">The if-elif-else Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#The_case_Statement\" title=\"The case Statement\">The case Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Numeric_Comparison\" title=\"Numeric Comparison\">Numeric Comparison<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#The_Command_aka_test\" title=\"The [ Command (aka test)\">The [ Command (aka test)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#-f_and_-d_for_%E2%80%98file_or_%E2%80%98directory_checks\" title=\"-f and -d for &#8216;file&#8217; or &#8216;directory&#8217; checks\">-f and -d for &#8216;file&#8217; or &#8216;directory&#8217; checks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#String_Comparison\" title=\"String Comparison\">String Comparison<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_if_a_File_Exists_and_Is_Readable\" title=\"Check if a File Exists and Is Readable\">Check if a File Exists and Is Readable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_if_a_Directory_Exists_and_Is_Writable\" title=\"Check if a Directory Exists and Is Writable\">Check if a Directory Exists and Is Writable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_if_a_String_Is_Empty_or_non-empty\" title=\"Check if a String Is Empty or non-empty\">Check if a String Is Empty or non-empty<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Compare_Strings_for_Equality\" title=\"Compare Strings for Equality\">Compare Strings for Equality<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_if_a_Variable_Is_Numeric\" title=\"Check if a Variable Is Numeric\">Check if a Variable Is Numeric<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_if_a_Command_Succeeded\" title=\"Check if a Command Succeeded\">Check if a Command Succeeded<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_if_a_File_Is_Executable\" title=\"Check if a File Is Executable\">Check if a File Is Executable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Compare_Numeric_Values\" title=\"Compare Numeric Values\">Compare Numeric Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Nested_Conditionals\" title=\"Nested Conditionals\">Nested Conditionals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Advanced_File_Checks\" title=\"Advanced File Checks\">Advanced File Checks<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#-ef_checks\" title=\"-ef checks\">-ef checks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#-nt_and_-lt_checks_for_files_date_comparion\" title=\"-nt and -lt checks for files&#8217; date comparion\">-nt and -lt checks for files&#8217; date comparion<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Check_the_status_of_Shell_Options_and_Variables\" title=\"Check the status of Shell Options and Variables\">Check the status of Shell Options and Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#String_Equality_and_Inequality\" title=\"String Equality and Inequality\">String Equality and Inequality<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Lexicographic_String_Comparison\" title=\"Lexicographic String Comparison\">Lexicographic String Comparison<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_if_Statement\"><\/span>The <code>if<\/code> Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>if<\/code> statement is the simplest form of a conditional statement in Bash. It executes a code only if a specific condition evaluates to true.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; condition ]]; then\n    # execute the code if the condition is true\nfi<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; 7 -eq 7 ]]; then\n    echo \"Number is 7\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number is 7<\/code><\/pre>\n\n\n\n<p>In this example, the condition <code>7 -eq 7<\/code> evaluates to true, so the code block inside the <code>if<\/code> statement is executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_if-else_Statement\"><\/span>The <code>if-else<\/code> Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>if-else<\/code> statement extends the <code>if<\/code> statement. It executes different code blocks based on whether the condition is true or false.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; condition ]]; then\n    # if-code to execute if the 'condition' is true\nelse\n    # else-code to execute if the 'condition' is false\nfi<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; 5 -gt 10 ]]; then\n    echo \"Number is greater than 10\"\nelse\n    echo \"Number is not greater than 10\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number is not greater than 10<\/code><\/pre>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<p>Since the condition <code>5 -gt 10<\/code> is false, the code block inside the <code>else<\/code> branch is executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_if-elif-else_Statement\"><\/span>The <code>if-elif-else<\/code> Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>if-elif-else<\/code> statement allows you to test multiple conditions in sequence and execute different code blocks based on the <strong>first true condition<\/strong> encountered.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; condition1 ]]; then\n    # Code to execute if condition1 is true\nelif &#091;&#091; condition2 ]]; then\n    # Code to execute if condition2 is true\nelse\n    # Code to execute if no conditions are true\nfi<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; 18 -lt 18 ]]; then\n    echo \"You are a minor\"\nelif &#091;&#091; 18 -eq 18 ]]; then\n    echo \"You just turned 18\"\nelse\n    echo \"You are an adult\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>You just turned 18<\/code><\/pre>\n\n\n\n<p>In this case, the first condition (<code>18 -lt 18<\/code>) is false, but the second condition (<code>18 -eq 18<\/code>) is true, so the code block corresponding to the <code>elif<\/code> branch is executed.<\/p>\n\n\n\n<p><code>-gt<\/code>: greater than<br><code>-ge<\/code>: greater than or equal to<br><code>-lt<\/code>: less than<br><code>-le<\/code>: less than or equal to<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_case_Statement\"><\/span>The <code>case<\/code> Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>case<\/code> statement provides a way to test multiple conditions against a single value. It is particularly useful when you want to compare a variable against multiple possible values. Here&#8217;s the basic structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>case \"$variable\" in\n    \"value1\")\n        # Code1 to execute for value1\n        ;;\n    \"value2\")\n        # Code2 to execute for value2\n        ;;\n    *)\n        # Code3 to execute for all other cases\n        ;;\nesac<\/code><\/pre>\n\n\n\n<p>Let&#8217;s consider an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruit=\"apple\"\ncase $fruit in\n    \"apple\")\n        echo \"It's an apple\";;\n    \"banana\")\n        echo \"It's a banana\";;\n    *)\n        echo \"Unknown fruit\";;\nesac<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>It's an apple<\/code><\/pre>\n\n\n\n<p>In this case, since the value of the <code>fruit<\/code> variable is &#8220;apple&#8221;, the corresponding code block is executed.<\/p>\n\n\n\n<p><strong>Note<\/strong>: You need to use <code>*<\/code> not <code>\"*\"<\/code> since <code>*<\/code> is not a string here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Numeric_Comparison\"><\/span>Numeric Comparison<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use numeric comparisons to check relationships between numbers. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; 15 -lt 20 ]]; then\n    echo \"15 is less than 20\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15 is less than 20<\/code><\/pre>\n\n\n\n<p>In this example, the condition <code>15 -lt 20<\/code> is true, so the code block is executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Command_aka_test\"><\/span>The <code>[<\/code> Command (aka <code>test<\/code>)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>[<\/code> command and <code>test<\/code> are basically the same. For example, <code>test -f &lt;file_name><\/code> and <code>[ -f &lt;file_name>]<\/code> are the same.<\/p>\n\n\n\n<p>The <code>[<\/code> command is a program that evaluates conditional expressions (<code>-n<\/code>, <code>-z<\/code>, <code>-r<\/code>, etc. given below). It is commonly used in conditional statements. For examples see the headings given below.<\/p>\n\n\n\n<p>However, please note that for more advanced conditional tests, it would be more beneficial to use <code>[[]]<\/code> and <code>(())<\/code>. In my opinion, stick with <strong>double brackets because they work most of the time<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"-f_and_-d_for_%E2%80%98file_or_%E2%80%98directory_checks\"><\/span><code>-f<\/code> and <code>-d<\/code> for &#8216;file&#8217; or &#8216;directory&#8217; checks <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use the <code>-f<\/code> and <code>-d<\/code> flags with the <code>[<\/code> command to check if a file exists (<code>-f<\/code>) or if a directory exists (<code>-d<\/code>). For instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;ajay@legion bash]$ ls -1\nbash_articles.md\nbash_brace.md\nbash_conditionals.md\nbash_loops_article.md\nbash_loops.md\nbash_parameter_substitution.md\ntemp.md\ntestdir<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091; -f \"bash_brace.md\" ]; then\n    echo \"File exists: bash_brace.md\"\nfi\n\nif &#091; -d \"testdir\" ]; then\n    echo \"Directory exists: testdir\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File exists: bash_brace.md\nDirectory exists: testdir<\/code><\/pre>\n\n\n\n<p>In the first example, the <code>-f<\/code> flag checks if the file &#8220;bash_brace.md&#8221; exists, while in the second example, the <code>-d<\/code> flag checks for the existence of the directory &#8220;testdir&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"String_Comparison\"><\/span>String Comparison<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>String comparisons allow you to compare two strings for equality or inequality. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091; \"smart\" = \"tech101\" ]; then\n    echo \"Strings are equal\"\nelse\n    echo \"Strings are not equal\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Strings are not equal<\/code><\/pre>\n\n\n\n<p>In this case, the strings &#8220;smart&#8221; and &#8220;tech101&#8221; are not equal, so the code block in the <code>else<\/code> branch is executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_if_a_File_Exists_and_Is_Readable\"><\/span>Check if a File Exists and Is Readable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use the <code>-r<\/code> flag with the <code>[[<\/code> command to check if a file exists and is readable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;ajay@legion bash]$ ls -1\nbash_articles.md\nbash_brace.md\nbash_conditionals.md\nbash_loops_article.md\nbash_loops.md\nbash_parameter_substitution.md\ntemp.md\ntestdir<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; -r \"bash_brace.md\" ]]; then\n    echo \"File exists and is readable\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File exists and is readable<\/code><\/pre>\n\n\n\n<p>The <code>-r<\/code> flag checks if the file &#8220;bash_brace.md&#8221; exists and has read permission.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_if_a_Directory_Exists_and_Is_Writable\"><\/span>Check if a Directory Exists and Is Writable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can combine the <code>-d<\/code> and <code>-w<\/code> flags to check if a directory exists and is writable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -al        \ntotal 96\ndrwxr-xr-x  3 ajay ajay  4096 Aug  8 14:05 .\ndrwxr-xr-x 28 ajay ajay 16384 Aug  9 09:24 ..\n-rw-r--r--  1 ajay ajay 11024 Aug  6 15:31 bash_articles.md\n-rw-r--r--  1 ajay ajay  7087 Aug  3 14:19 bash_brace.md\ndrwxr-xr-x  2 ajay ajay  4096 Aug  8 14:05 testdir<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; -d \"testdir\" &amp;&amp; -w \"testdir\" ]]; then\n    echo \"Directory exists and is writable\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Directory exists and is writable<\/code><\/pre>\n\n\n\n<p>In this example, the script checks if both the directory &#8220;testdir&#8221; exists (<code>-d<\/code> flag) and has write permission (<code>-w<\/code> flag).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_if_a_String_Is_Empty_or_non-empty\"><\/span>Check if a String Is Empty or non-empty<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Use <code>-z<\/code> and <code>-n<\/code>. Both are exactly opposite of each other.<\/p>\n\n\n\n<p>Just remember the following to avoid the confusion:<br><code>-z<\/code> means &#8220;null&#8221;<br><code>-n<\/code> means &#8220;not null&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo=\"bar\"\nif &#091;&#091; -n \"$foo\" ]]; then\n    echo \"foo is not null\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo is not null<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>foo=\"bar\"\nif &#091;&#091; -z \"$foo\" ]]; then\n    echo \"foo is null\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo=\"\"\nif &#091;&#091; -n \"$foo\" ]]; then\n    echo \"foo is not null\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo=\"\"\nif &#091;&#091; -z \"$foo\" ]]; then\n    echo \"foo is null\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo is null<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Compare_Strings_for_Equality\"><\/span>Compare Strings for Equality<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can compare two strings for equality using the <code>==<\/code> operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str1=\"smarttech101\"\nstr2=\"smarttech101\"\nif &#091;&#091; \"$str1\" == \"$str2\" ]]; then\n    echo \"Strings are equal\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Strings are equal<\/code><\/pre>\n\n\n\n<p>In this example, the string variables &#8220;str1&#8221; and &#8220;str2&#8221; give equal strings, so the code block is executed.<\/p>\n\n\n\n<p><a href=\"https:\/\/smarttech101.com\/how-to-create-shell-scripts-in-linux-unix\/#variables_and_basic_commands_in_bash_shell_in_linux\">For variable expansion in bash, look over here<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_if_a_Variable_Is_Numeric\"><\/span>Check if a Variable Is Numeric<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use regular expressions to check if a variable contains only numeric characters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var=\"101\"\nif &#091;&#091; \"$var\" =~ ^&#091;0-9]+$ ]]; then\n    echo \"Variable is numeric\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Variable is numeric<\/code><\/pre>\n\n\n\n<p>The regular expression <code>^[0-9]+$<\/code> matches strings that consist of one or more digits.<\/p>\n\n\n\n<p><a href=\"https:\/\/smarttech101.com\/regular-expression-regex-and-regexp-in-linux-ft-grep\/\">To learn about regular expression, look at this article<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_if_a_Command_Succeeded\"><\/span>Check if a Command Succeeded<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use the exit status of a command to determine if it succeeded. If the exit status is zero, the command succeeded:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if command; then\n    echo \"Command succeeded\"\nfi<\/code><\/pre>\n\n\n\n<p>Output (assuming <code>command<\/code> succeeds):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Command succeeded<\/code><\/pre>\n\n\n\n<p>In this case, the <code>command<\/code> is executed, and if it exits with a status of zero, the code block is executed.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>filename=\"$HOME\/Pictures\/example.png\"\n\nif wget -qO \"$filename\" \"https:\/\/smarttech101.com\/test.png\"\nthen\n  notify-send \"Image Downloaded\"\nelse\n  notify-send \"Image Download Failed\"\nfi<\/code><\/pre>\n\n\n\n<p>If <code>wget<\/code> is unable to download the image, it will fail and notification is popped-up in your Linux machine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_if_a_File_Is_Executable\"><\/span>Check if a File Is Executable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use the <code>-x<\/code> flag to check if a file is executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;ajay@legion .my_scripts]$ ls -al test.sh\n-rwxr--r-- 1 ajay ajay 315 Aug  4 18:31 test.sh<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#091;&#091; -x \"test.sh\" ]]; then\n    echo \"File is executable\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File is executable<\/code><\/pre>\n\n\n\n<p>The <code>-x<\/code> flag tests if the file &#8220;test.sh&#8221; has execute permission.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Compare_Numeric_Values\"><\/span>Compare Numeric Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use arithmetic evaluation to compare numeric values using operators like <code>&lt;<\/code>, <code>&gt;<\/code>, <code>&lt;=<\/code>, <code>&gt;=<\/code>, <code>==<\/code>, and <code>!=<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (( 5 &gt; 3 )); then\n    echo \"5 is greater than 3\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5 is greater than 3<\/code><\/pre>\n\n\n\n<p>In this example, the arithmetic expression <code>5 &gt; 3<\/code> is true, so the code block is executed.<\/p>\n\n\n\n<p>Similarly, <code>&lt;<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nested_Conditionals\"><\/span>Nested Conditionals<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can nest conditional statements within each other to create more complex decision structures. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var=10\nif &#091;&#091; $var -gt 5 ]]; then\n    if &#091;&#091; $var -lt 100 ]]; then\n        echo \"$var is in between 5 and 100\"\n    fi\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 is in between 5 and 100<\/code><\/pre>\n\n\n\n<p>In this example, both <code>-gt 5<\/code> and <code>-lt 100<\/code> are true, so the nested code block is executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advanced_File_Checks\"><\/span>Advanced File Checks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"-ef_checks\"><\/span><code>-ef<\/code> checks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Bash provides additional checks to examine files in detail. You can use the <code>-ef<\/code> flag to check two files have same device and inode numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file1=\"file.txt\"\nfile2=\"link_to_file.txt\"\n\ntouch \"$file1\"\nln -s \"$file1\" \"$file2\"\n\nif &#091;&#091; \"$file1\" -ef \"$file2\" ]]; then\n    echo \"file1 and file2 with same device and inode numbers\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file1 and file2 with same device and inode numbers<\/code><\/pre>\n\n\n\n<p>In this example, both <code>file1<\/code> and <code>file2<\/code> point to the same content, so the <code>-ef<\/code> check succeeds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"-nt_and_-lt_checks_for_files_date_comparion\"><\/span><code>-nt<\/code> and <code>-lt<\/code> checks for files&#8217; date comparion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also compare file modification times using the <code>-nt<\/code> (newer than) and <code>-ot<\/code> (older than) flags:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file1=\"file1.txt\"\nfile2=\"file2.txt\"\n\ntouch \"$file1\"\nsleep 2\ntouch \"$file2\"\n\nif &#091;&#091; \"$file2\" -nt \"$file1\" ]]; then\n    echo \"file2 is newer than file1\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file2 is newer than file1<\/code><\/pre>\n\n\n\n<p>In this example, <code>file2<\/code> was touched more recently than <code>file1<\/code>, so the <code>-nt<\/code> check returns true.<\/p>\n\n\n\n<p>Conversely, you can use the <code>-ot<\/code> flag to check if a file is older than another:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file1=\"file1.txt\"\nfile2=\"file2.txt\"\n\ntouch \"$file1\"\nsleep 2\ntouch \"$file2\"\n\nif &#091;&#091; \"$file1\" -ot \"$file2\" ]]; then\n    echo \"file1 is older than file2\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file1 is older than file2<\/code><\/pre>\n\n\n\n<p>In this scenario, <code>file1<\/code> was created earlier than <code>file2<\/code>, making the <code>-ot<\/code> check true.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_the_status_of_Shell_Options_and_Variables\"><\/span>Check the status of Shell Options and Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use conditional statements to check the status of shell options and variables. For instance, you can check if a shell option is enabled:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>optionname=\"emacs\"\n\nif &#091;&#091; -o \"$optionname\" ]]; then\n    echo \"Shell option '$optionname' is enabled\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Shell option 'emacs' is enabled<\/code><\/pre>\n\n\n\n<p>By default, my bash shell have enabled emacs mode, so testing above option turns out to be true and I get the given output. For <code>optionname=\"vi\"<\/code>, no output is generated, because I have not enabled <code>vi<\/code> mode.<\/p>\n\n\n\n<p>Similarly, you can check if a shell variable is set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myvar=\"Hello\"\n\nif &#091;&#091; -v \"myvar\" ]]; then\n    echo \"Shell variable 'myvar' is set\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Shell variable 'myvar' is set<\/code><\/pre>\n\n\n\n<p>In this example, the script verifies if the shell variable <code>myvar<\/code> is set.<\/p>\n\n\n\n<p>You can also determine if a shell variable is a name reference:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>declare -n refvar=myvar\n\nif &#091;&#091; -R \"refvar\" ]]; then\n    echo \"Shell variable 'refvar' is a name reference\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Shell variable 'refvar' is a name reference<\/code><\/pre>\n\n\n\n<p>In this example, the script checks if the variable <code>refvar<\/code> is a name reference to another variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"String_Equality_and_Inequality\"><\/span>String Equality and Inequality<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can compare strings for equality using the<\/p>\n\n\n\n<p><code>=<\/code> operator, or for inequality using <code>!=<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1=\"smarttech101\"\nstring2=\"smarttech101\"\n\nif &#091;&#091; \"$string1\" == \"$string2\" ]]; then\n    echo \"Strings are equal\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Strings are equal<\/code><\/pre>\n\n\n\n<p>In this case, the strings <code>smarttech101<\/code> and <code>smarttech101<\/code> are equal, so the code block executes.<\/p>\n\n\n\n<p>On the other hand, checking for inequality:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1=\"smart\"\nstring2=\"tech101\"\n\nif &#091;&#091; \"$string1\" != \"$string2\" ]]; then\n    echo \"Strings are not equal\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Strings are not equal<\/code><\/pre>\n\n\n\n<p>Since the two strings are not equal, the code block within the <code>if<\/code> statement runs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Lexicographic_String_Comparison\"><\/span>Lexicographic String Comparison<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can compare strings lexicographically using operators like <code>&lt;<\/code> and <code>&gt;<\/code> to determine their order. For example, comparing <code>string1<\/code> and <code>string2<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1=\"smart\"\nstring2=\"tech101\"\n\nif &#091;&#091; \"$string1\" &lt; \"$string2\" ]]; then\n    echo \"string1 sorts before string2\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1 sorts before string2<\/code><\/pre>\n\n\n\n<p>In this scenario, &#8220;smart&#8221; appears before &#8220;tech101&#8221; in lexicographic order, so the code block executes.<\/p>\n\n\n\n<p>Similarly, if <code>string1<\/code> is lexicographically greater than <code>string2<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1=\"tech101\"\nstring2=\"smart\"\n\nif &#091;&#091; \"$string1\" &gt; \"$string2\" ]]; then\n    echo \"string1 sorts after string2\"\nfi<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1 sorts after string2<\/code><\/pre>\n\n\n\n<p>Here, &#8220;tech101&#8221; comes after &#8220;smart,&#8221; so the code block runs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>That&#8217;s all folks. Thanks for staying this long. If you any question and\/or comments put them below. <a href=\"https:\/\/www.gnu.org\/s\/bash\/manual\/html_node\/Bash-Conditional-Expressions.html\">If you want to learn more, read the official documentation about the conditional statements in bash over here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about conditonal statements in bash &#8211; if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `>`, etc. with examples.<\/p>\n","protected":false},"author":2,"featured_media":2966,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[58],"tags":[18,35],"class_list":["post-2965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","tag-command-line-tools","tag-core-linux-utilities"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101<\/title>\n<meta name=\"description\" content=\"Learn about conditonal statements in bash - if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `&gt;`, etc. with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101\" \/>\n<meta property=\"og:description\" content=\"Learn about conditonal statements in bash - if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `&gt;`, etc. with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\" \/>\n<meta property=\"og:site_name\" content=\"SmartTech101\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-11T09:13:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-11T09:13:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ajay\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ajay_yadav\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay\" \/>\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:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\"},\"author\":{\"name\":\"Ajay\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334\"},\"headline\":\"How to Use Conditional Statements in Bash: The If-Else Fi Basics\",\"datePublished\":\"2023-08-11T09:13:41+00:00\",\"dateModified\":\"2023-08-11T09:13:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\"},\"wordCount\":1245,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633\"},\"image\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1\",\"keywords\":[\"Command Line Tools\",\"core linux utilities\"],\"articleSection\":[\"bash\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\",\"url\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\",\"name\":\"How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101\",\"isPartOf\":{\"@id\":\"https:\/\/smarttech101.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1\",\"datePublished\":\"2023-08-11T09:13:41+00:00\",\"dateModified\":\"2023-08-11T09:13:44+00:00\",\"description\":\"Learn about conditonal statements in bash - if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `>`, etc. with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1\",\"width\":1280,\"height\":720,\"caption\":\"In this article, you would learn about if-else-elif etc., conditional expressions like -z, -n, etc. with examples.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/smarttech101.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use Conditional Statements in Bash: The If-Else Fi Basics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/smarttech101.com\/#website\",\"url\":\"https:\/\/smarttech101.com\/\",\"name\":\"SmartTech101\",\"description\":\"Do Everything in Linux\",\"publisher\":{\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/smarttech101.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633\",\"name\":\"Ajay Yadav\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1\",\"width\":180,\"height\":60,\"caption\":\"Ajay Yadav\"},\"logo\":{\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334\",\"name\":\"Ajay\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g\",\"caption\":\"Ajay\"},\"sameAs\":[\"https:\/\/x.com\/ajay_yadav\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101","description":"Learn about conditonal statements in bash - if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `>`, etc. with examples.","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:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101","og_description":"Learn about conditonal statements in bash - if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `>`, etc. with examples.","og_url":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/","og_site_name":"SmartTech101","article_published_time":"2023-08-11T09:13:41+00:00","article_modified_time":"2023-08-11T09:13:44+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png","type":"image\/png"}],"author":"Ajay","twitter_card":"summary_large_image","twitter_creator":"@ajay_yadav","twitter_misc":{"Written by":"Ajay","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#article","isPartOf":{"@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/"},"author":{"name":"Ajay","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334"},"headline":"How to Use Conditional Statements in Bash: The If-Else Fi Basics","datePublished":"2023-08-11T09:13:41+00:00","dateModified":"2023-08-11T09:13:44+00:00","mainEntityOfPage":{"@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/"},"wordCount":1245,"commentCount":1,"publisher":{"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633"},"image":{"@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1","keywords":["Command Line Tools","core linux utilities"],"articleSection":["bash"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/","url":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/","name":"How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101","isPartOf":{"@id":"https:\/\/smarttech101.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage"},"image":{"@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1","datePublished":"2023-08-11T09:13:41+00:00","dateModified":"2023-08-11T09:13:44+00:00","description":"Learn about conditonal statements in bash - if, if else, if-elif-else, conditional expressions like `-z`, `-n`, `>`, etc. with examples.","breadcrumb":{"@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#primaryimage","url":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1","contentUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1","width":1280,"height":720,"caption":"In this article, you would learn about if-else-elif etc., conditional expressions like -z, -n, etc. with examples."},{"@type":"BreadcrumbList","@id":"https:\/\/smarttech101.com\/how-to-use-conditional-statements-in-bash-the-if-else-fi-basics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smarttech101.com\/"},{"@type":"ListItem","position":2,"name":"How to Use Conditional Statements in Bash: The If-Else Fi Basics"}]},{"@type":"WebSite","@id":"https:\/\/smarttech101.com\/#website","url":"https:\/\/smarttech101.com\/","name":"SmartTech101","description":"Do Everything in Linux","publisher":{"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smarttech101.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633","name":"Ajay Yadav","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/image\/","url":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1","contentUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1","width":180,"height":60,"caption":"Ajay Yadav"},"logo":{"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/image\/"}},{"@type":"Person","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334","name":"Ajay","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g","caption":"Ajay"},"sameAs":["https:\/\/x.com\/ajay_yadav"]}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Conditional-Statements-in-Bash-The-If-Else-Fi-Basics.png?fit=1280%2C720&ssl=1","_links":{"self":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts\/2965"}],"collection":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/comments?post=2965"}],"version-history":[{"count":3,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts\/2965\/revisions"}],"predecessor-version":[{"id":2969,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts\/2965\/revisions\/2969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/media\/2966"}],"wp:attachment":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/media?parent=2965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/categories?post=2965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/tags?post=2965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}