{"id":25051,"date":"2023-10-17T08:31:27","date_gmt":"2023-10-17T08:31:27","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=25051"},"modified":"2024-01-23T06:12:18","modified_gmt":"2024-01-23T06:12:18","slug":"bash-if-variable-exists","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/","title":{"rendered":"Check If a Variable Exists in Bash Using If Statement"},"content":{"rendered":"<p style=\"text-align: justify;\">In Bash, the &#8220;if else&#8221; statement is used to control the flow of execution of the bash script statements. Within the &#8220;if else&#8221; conditional statement, a variable is an essential factor to keep the input values that help to run the script based on the variables set. Therefore, checking if a variable exists is an important task in bash scripting. There are different options including <strong>-z<\/strong>, <strong>-v<\/strong>, <strong>-n,<\/strong> <strong>-p<\/strong>, and modifiers that assist in accomplishing this task.<\/p>\n<p style=\"text-align: justify;\">This article delves into four methods to check if a variable exists in Bash using the &#8220;if else&#8221; statement.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Practice_files_bash-if-variable-exists.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 to Check if a Variable Exists 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\/if-else\/if-variable-exists\/#What_is_Variable_in_Bash\" >What is Variable 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\/if-else\/if-variable-exists\/#Types_of_Variables\" >Types of Variables<\/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\/if-else\/if-variable-exists\/#4_Methods_to_Check_if_a_Variable_Exists_in_Bash\" >4 Methods to Check if a Variable Exists 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\/if-else\/if-variable-exists\/#1_Using_%E2%80%9C-v%E2%80%9D_Operator\" >1. Using &#8220;-v&#8221; Operator<\/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\/if-else\/if-variable-exists\/#2_Using_%E2%80%9C-z%E2%80%9D_Operator\" >2. Using &#8220;-z&#8221; Operator<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#A_Check_If_Integer_Variable_is_Empty\" >A. Check If Integer Variable is Empty<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#B_Check_If_String_Variable_is_Set_and_Not_Empty_by_Parameter_Expansion\" >B. Check If String Variable is Set and Not Empty by Parameter Expansion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#C_Check_If_Integer_Variable_is_Set_and_Not_Empty_Using_Parameter_Expansion\" >C. Check If Integer Variable is Set and Not Empty Using Parameter Expansion<\/a><\/li><\/ul><\/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\/if-else\/if-variable-exists\/#3_Using_%E2%80%9C-n%E2%80%9D_Operator\" >3. Using &#8220;-n&#8221; Operator<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#A_Check_If_Integer_Variable_is_Not_Empty_in_Bash\" >A. Check If Integer Variable is Not Empty in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#B_Check_for_Environment_Variable_Using_%E2%80%9Cenv%E2%80%9D_Command\" >B. Check for Environment Variable Using &#8220;env&#8221; Command.<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#C_Check_for_Variable_Using_%E2%80%9Cprintenv%E2%80%9D_Command\" >C. Check for Variable Using &#8220;printenv&#8221; Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#D_Check_for_Variable_Using_%E2%80%9Cecho%E2%80%9D_Command_in_Bash\" >D. Check for Variable Using &#8220;echo&#8221; Command in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#E_Using_the_%E2%80%9Cisset%E2%80%9D_Function_to_Check_If_a_Variable_is_Set\" >E. Using the &#8220;isset&#8221; Function to Check If a Variable is Set<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#F_Check_for_Variable_Existence_Using_Parameter_Expansion\" >F. Check for Variable Existence Using Parameter Expansion<\/a><\/li><\/ul><\/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\/if-else\/if-variable-exists\/#4_Using_%E2%80%9Cdeclare%E2%80%9D_Command\" >4. Using &#8220;declare&#8221; Command<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#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-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#How_to_check_if_a_character_exists_in_a_string_in_bash\" >How to check if a character exists in a string in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#What_is_%E2%80%98_in_bash\" >What is \u2018${}\u2019 in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#How_to_read_a_variable_in_bash\" >How to read a variable in bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Variable_in_Bash\"><\/span>What is Variable in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/variable-in-programming\/\" target=\"_blank\" rel=\"noopener\">variable<\/a><\/u><\/strong> is a vital component of any programming language. It is a storage that stores values temporarily. These values can be any type including<strong> integers, floating numbers, strings, or any characters<\/strong>. In <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">bash scripting<\/a><\/u><\/strong>, you can define a variable by any name you wish for or keep any value. You can access the stored values by prefixing <strong>$<\/strong> using the variable name in bash. Check the syntax below:<\/p>\n<p><code class=\"\" data-line=\"\">variable_name=value<\/code><br \/>\nLet\u2019s see an example of it:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">number=10\nwebsite_name=&quot;Linuxsimply.com&quot;<\/code><\/pre>\n<p>Here, the <strong>number<\/strong> and <strong>website_name<\/strong> are the variable names that store integer <strong>10<\/strong> and a string <strong>Linuxsimply.com<\/strong> respectively.<\/p>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#e8f9fc;border-color:#ffffff;color:#333333;\"><strong>Note:<\/strong> You should write strings within a double quotation mark to assign them to variables.<\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Types_of_Variables\"><\/span>Types of Variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Different types of variables store different types of data in bash. Primarily, there are <strong>three<\/strong> types:<\/p>\n<ol>\n<li><strong>User-defined Variables:<\/strong>\n<p style=\"text-align: justify;\">The user defines this type of variable to <strong>assign<\/strong> a value. Users can assign integers, numbers, strings, or arrays as a variable. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">name=&quot;Hello, I am mou&quot;<\/code><\/pre>\n<\/li>\n<li><strong>Environment Variables:<\/strong>\n<p style=\"text-align: justify;\"><strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/environment-variables\/\" target=\"_blank\" rel=\"noopener\"> Environment variables<\/a><\/u><\/strong> are predefined by the <strong>system<\/strong>. Linux <strong>bash shell<\/strong> controls the environment variable. These variables are always written in <strong>capital letters<\/strong>. Let\u2019s see an example of an environment variable:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;$USER&quot;<\/code><\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25056\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/1.user-name.png\" alt=\"checking user name with environment variable\" width=\"422\" height=\"64\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/1.user-name.png 422w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/1.user-name-300x45.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/p>\n<p>It shows the current user name <strong>mou<\/strong>.<\/li>\n<li><strong>Special Variables: <\/strong><br \/>\n<strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/special-variables\/\" target=\"_blank\" rel=\"noopener\">Special variables<\/a><\/u><\/strong> store<strong> predefined values<\/strong> and it helps users to write the bash script easily. Now, see an example of using a special variable:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\necho &quot;number of arguments:$#&quot;<\/code><\/pre>\n<p>Here, the <strong>$#<\/strong> special variable represents the number of arguments.\u00a0<img decoding=\"async\" class=\"aligncenter size-full wp-image-25057\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/2.special-variable.png\" alt=\"example of special variable\" width=\"700\" height=\"64\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/2.special-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/2.special-variable-300x27.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>You can see the number of arguments <strong>3<\/strong> in the place of the <strong>$#<\/strong> sign.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"4_Methods_to_Check_if_a_Variable_Exists_in_Bash\"><\/span>4 Methods to Check if a Variable Exists in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To check if a variable exists in Bash, there are four approaches utilizing the &#8220;if else&#8221; statement along with various options. You can use different options like <strong>-v<\/strong>, <strong>-z<\/strong>, <strong>-n<\/strong>, and <strong>-p<\/strong> to check the presence of variables. In addition to the &#8220;-z&#8221; and &#8220;-p&#8221; options, each method includes distinct scenarios for checking the existence of a variable in Bash. Depending on the conditions set using the options, the output returns as<strong> true <\/strong>or <strong>false<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_%E2%80%9C-v%E2%80%9D_Operator\"><\/span>1. Using &#8220;-v&#8221; Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To check if a vari<span style=\"font-size: inherit;\">able <\/span><span style=\"font-size: inherit;\">exists in the ba<\/span><span style=\"font-size: inherit;\">sh, you can use<\/span>\u00a0the <strong>-v<\/strong> <strong>operator<\/strong> that checks the <span style=\"font-size: inherit;\">variable\u2019s existence regardless of whether the variable is <\/span><strong style=\"font-size: inherit;\">empty<\/strong><span style=\"font-size: inherit;\">. <\/span><span style=\"font-size: inherit;\">To check if a variable exists in Bash, follow the procedure:<\/span><\/p>\n<ol>\n<li>Open the <strong>Ubuntu terminal<\/strong> and create a Bash file with Nano text editor (for example: <code class=\"\" data-line=\"\">nano v_opt.sh<\/code>)<strong><u><br \/>\n<\/u><\/strong><\/li>\n<li>Now, write the following script in the Bash file:\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nnumber=10\nif [[ -v number ]]; then\necho &quot;variable exists.&quot;\nelse\necho &quot;variable does not exist.&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\">\nThe <code class=\"\" data-line=\"\">-v <\/code>operator is used to check if a variable exists. Here, variable <code class=\"\" data-line=\"\">number=10<\/code>. If you run the script, it&#8217;ll show that the &#8220;variable exists.&#8221; If the variable was not set, the script would show &#8220;variable does not exist.&#8221;<br \/>\n<\/div><\/div><\/li>\n<li>After that, <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/basics\/executing\/make-file-executable\/\" target=\"_blank\" rel=\"noopener\">make the script executable.<\/a><\/u><\/strong> and run it.<br \/>\n<img decoding=\"async\" class=\"aligncenter wp-image-25058 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/3.v-operator.png\" alt=\"checking if a variable exists in bash if statement using -v operator\" width=\"394\" height=\"68\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/3.v-operator.png 394w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/3.v-operator-300x52.png 300w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/> In this image, you can see that the <strong>variable exists<\/strong>.<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_%E2%80%9C-z%E2%80%9D_Operator\"><\/span>2. Using &#8220;-z&#8221; Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can check if a variable exists using the <strong>-z<\/strong> <strong>option<\/strong> within the &#8220;if else&#8221; statement. The &#8220;if else&#8221; statement returns <strong>true<\/strong> when the variable is <strong>empty<\/strong>. It checks whether the variable is <strong>empty or not<\/strong>. With the help of <strong>-z<\/strong> <strong>operator<\/strong>, two distinct conditions can be built to check the existence of <strong>integer variables<\/strong> and <strong>string variables<\/strong>.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"A_Check_If_Integer_Variable_is_Empty\"><\/span>A. Check If Integer Variable is Empty<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">To check the existence of an integer variable, you can use the <strong>-z<\/strong> <strong>operator<\/strong> within the <strong>if else<\/strong> <strong>statement<\/strong>. It checks if the variable value is empty. The below script is an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nnumber=10\nif [ -z &quot;$number&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;variable does not exist.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable exists.&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>Here, the <code class=\"\" data-line=\"\">-z <\/code>operator is used to check if the defined <strong>number<\/strong> variable is empty. If the variable is <strong>empty<\/strong>, it will execute that the <strong>variable does not exist<\/strong>. Otherwise, it will execute that the <strong>variable exists<\/strong>.<\/p>\n<\/div><\/div>\n<p>Now, run the script.<b> <strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25059\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/4.z-operator.png\" alt=\"checking if a variable exists in bash if statement using -z operator\" width=\"394\" height=\"67\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/4.z-operator.png 394w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/4.z-operator-300x51.png 300w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/><\/strong>\u00a0<\/b>You can notice that the <strong>variable exists.<\/strong><\/p>\n<h4><span class=\"ez-toc-section\" id=\"B_Check_If_String_Variable_is_Set_and_Not_Empty_by_Parameter_Expansion\"><\/span>B. Check If String Variable is Set and Not Empty by Parameter Expansion<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">To check if a variable is <strong>set and not empty<\/strong>, you can adopt <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/parameter-expansion\/\" target=\"_blank\" rel=\"noopener\"><strong>parameter expansion<\/strong><\/a> with the help of the <strong>-z<\/strong> <strong>operator<\/strong>. The syntax <code class=\"\" data-line=\"\">&quot;${a+x}&quot;<\/code> inside the <strong>if<\/strong> condition replaces the string value with <strong>x<\/strong> if the variable <strong>a<\/strong> is <strong>set <\/strong>and<strong> non-empty<\/strong>. Then the -z operator evaluates to <strong>false<\/strong> if the variable value is not empty. Now write the following script to check if a string variable is set and not empty:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\na=&quot;Hello,how are you?&quot;\n\n# Check if the variable is set and not empty using parameter expansion\nif [ -z &quot;${a+x}&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;String variable is unset and empty&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;String variable is set and not empty&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\"> Here, the variable <code class=\"\" data-line=\"\">a=&quot;Hello,how are you?&quot;<\/code> holds a string. The <code class=\"\" data-line=\"\">-z &quot;$(a+x)<\/code> operator checks if the variable is set or unset and the length is <strong>zero or not. <\/strong><\/div><\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25060\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/5.parameter-expansion-output.png\" alt=\"checking if a variable exists in bash if statement using parameter expansion method\" width=\"585\" height=\"68\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/5.parameter-expansion-output.png 585w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/5.parameter-expansion-output-300x35.png 300w\" sizes=\"(max-width: 585px) 100vw, 585px\" \/> <\/strong>As the variable was set and not empty, the above script executed the else block of the script.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"C_Check_If_Integer_Variable_is_Set_and_Not_Empty_Using_Parameter_Expansion\"><\/span>C. Check If Integer Variable is Set and Not Empty Using Parameter Expansion<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">Using the <strong>-z<\/strong> <strong>option<\/strong> along with the <strong>+set<\/strong> <strong>modifier<\/strong> within the &#8220;if else&#8221; statement aids in finding variables in bash. The <code class=\"\" data-line=\"\">${x+set}<\/code> within the <strong>if<\/strong> condition replaces the integer value of the variable with <strong>set<\/strong> modifier if the variable is not empty. The <code class=\"\" data-line=\"\">-z<\/code> option returns <strong>false<\/strong> when the variable is set and not empty. Check the example below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nx=10\nif [ -z &quot;${x+set}&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;variable x is unset.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable x is set.&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>Here, the <code class=\"\" data-line=\"\">+set<\/code> modifier along with the <code class=\"\" data-line=\"\">-z<\/code> option checks the presence of a variable. If you declare the variable, the output will show that the &#8220;variable is set&#8221; otherwise, it will show that the &#8220;variable is not set&#8221;.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25067\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/12.z-option-with-set.png\" alt=\"checking variable using z option with set modifier\" width=\"390\" height=\"63\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/12.z-option-with-set.png 390w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/12.z-option-with-set-300x48.png 300w\" sizes=\"(max-width: 390px) 100vw, 390px\" \/>As the variable <code class=\"\" data-line=\"\">x<\/code> is declared in the bash script, the &#8220;else&#8221; block of the script runs and the output shows that the &#8220;variable is set&#8221;.<\/p>\n<p style=\"text-align: justify;\">Similiarly, the <strong>-unset<\/strong> <strong>modifier<\/strong> with the <strong>-z<\/strong> <strong>option<\/strong> checks if the variable is unset and null. If the variable <strong>x<\/strong> is unset and null, the <code class=\"\" data-line=\"\">{x-unset}<\/code> syntax replaces the value with the <strong>unset <\/strong>modifier, otherwise, it is not changed. That means it will execute the else block code if you define any variable that contains a value.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nx=\nif [ -z &quot;${x-unset}&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;variable x is unset.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable x is set.&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>In this bash script, the variable <strong>x<\/strong> is empty. Hence, it will execute the true block code <strong>\u2018<\/strong>variable x is unset\u2019. When the variable is not empty, it will return the <strong>false<\/strong> expression.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25068\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/13.z-option-with-unset-modifier.png\" alt=\"checking variable using z option with unset modifier\" width=\"422\" height=\"65\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/13.z-option-with-unset-modifier.png 422w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/13.z-option-with-unset-modifier-300x46.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/> As the variable <code class=\"\" data-line=\"\">x<\/code> doesn&#8217;t hold any value, the &#8220;if&#8221; block executes and the output shows that the &#8220;variable x is unset&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_%E2%80%9C-n%E2%80%9D_Operator\"><\/span>3. Using &#8220;-n&#8221; Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>-n<\/strong> <strong>operator<\/strong> in Bash is utilized to check if a variable exists by verifying whether its length is<strong> non-zero. <\/strong>When combined with an &#8220;if else&#8221; statement, it allows you to create conditional logic based on whether a variable has been defined or not.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"A_Check_If_Integer_Variable_is_Not_Empty_in_Bash\"><\/span>A. Check If Integer Variable is Not Empty in Bash<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">Write the following script in your Bash file to check if an integer variable is not empty:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nnumber=10\nif [ -n &quot;$number&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable exists.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;The variable does not exist.&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\">Here, the variable <code class=\"\" data-line=\"\">number<\/code> is assigned the value <strong>10<\/strong>. The condition <code class=\"\" data-line=\"\">-n $number<\/code> within the &#8220;if else&#8221; statement checks whether the length of the variable &#8216;$number&#8217; is non-zero.\u00a0<\/div><\/div>\n<p>Now, run the script.<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25061\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/6.n-operator.png\" alt=\"checking integer variable existence\" width=\"391\" height=\"69\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/6.n-operator.png 391w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/6.n-operator-300x53.png 300w\" sizes=\"(max-width: 391px) 100vw, 391px\" \/><\/p>\n<p style=\"text-align: justify;\">As the length of the variable <code class=\"\" data-line=\"\">\u00a0number<\/code> is non-zero, the <strong>&#8220;if&#8221;<\/strong> block executes and the output shows that<strong> &#8220;The variable exists.&#8221;<\/strong><\/p>\n<h4><span class=\"ez-toc-section\" id=\"B_Check_for_Environment_Variable_Using_%E2%80%9Cenv%E2%80%9D_Command\"><\/span>B. Check for Environment Variable Using &#8220;env&#8221; Command.<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">To check if an <strong>environment variable <\/strong>exists in Bash, you can use this <strong>env<\/strong> <strong>command<\/strong>. This command lists all the environment variables. Hence, the <strong>grep<\/strong> <strong>command<\/strong> is used along with <strong>env<\/strong> to find the defined variable in the environment variable\u2019s list.<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#E8F9FC;border-color:#ffffff;color:#333333;\"> <strong>Note:<\/strong> Ensure the variable is <strong>exported<\/strong> in the environment variable before running the script. Otherwise, the output will execute the<strong> false<\/strong> code block. <\/div><\/div>\n<p style=\"text-align: justify;\">Here&#8217;s an example script you can write and run:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nvariable=&quot;Hello,How are you?&quot;\nif [ -n &quot;$(env | grep -o &#039;^variable=&#039;)&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;variable is set.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable is unset.&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>Here, the environment variable named <code class=\"\" data-line=\"\">variable<\/code> is\u00a0 assigned with a string <code class=\"\" data-line=\"\">&quot;Hello, How are you?&quot;<\/code>. The condition <code class=\"\" data-line=\"\"> -n &quot;$(env | grep -o &#039;^variable=&#039;)&quot;<\/code> checks whether the environment variable named <strong>&#8220;<\/strong>variable<strong>&#8221;\u00a0<\/strong>is set or not. The <code class=\"\" data-line=\"\">env<\/code> command checks the environment variable&#8217;s existence. The <code class=\"\" data-line=\"\">grep<\/code> command finds the line that starts with a variable to ensure the existence of the variable. <\/div><\/div>\n<p style=\"text-align: justify;\">If you run the script, then the output will be like below.<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25062\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/7.checking-variable-existence-using-env.png\" alt=\"checking if environment variable existence using env command\" width=\"361\" height=\"68\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/7.checking-variable-existence-using-env.png 361w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/7.checking-variable-existence-using-env-300x57.png 300w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/> As the environment variable is set, the &#8220;if&#8221; block of the script runs and the output shows that the &#8220;<strong>variable is set<\/strong>.&#8221;<\/p>\n<h4><span class=\"ez-toc-section\" id=\"C_Check_for_Variable_Using_%E2%80%9Cprintenv%E2%80%9D_Command\"><\/span>C. Check for Variable Using &#8220;printenv&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">While dealing with a lot of external variables and other methods fail to check the particular environment variable, implementing this method is the best possible option. However, it is <strong>limited<\/strong> to environment variables, in that case, check other methods if you want to check other types of variables. To check if a variable is set using <strong>printenv<\/strong> <strong>command<\/strong>, write the following script in your Bash file:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nvariable=&quot;Hello,How are you?&quot;\nif [ -n &quot;$(printenv variable)&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;variable is set.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable is unset.&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\">The statement <code class=\"\" data-line=\"\">-n &quot;$(printenv variable)&quot;<\/code> checks whether the length of the string obtained by executing the <code class=\"\" data-line=\"\">printenv<\/code> command on the variable named\u00a0 <code class=\"\" data-line=\"\">variable<\/code>\u00a0 is nono-zero.\u00a0<\/div><\/div>\n<p style=\"text-align: justify;\">Before running the script, you have to set the variable to the environment variable by writing the following code in your <strong>Ubuntu<\/strong> terminal:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">export variable=&quot;Hello,How are you?&quot;<\/code><\/pre>\n<p>Now, run the script.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/8.exporting-environemnt-variable.png\" alt=\"checking if environment variable exists in bash if statement \" width=\"700\" height=\"86\" \/><br \/>\nYou can see that the environment variable is set in your system.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"D_Check_for_Variable_Using_%E2%80%9Cecho%E2%80%9D_Command_in_Bash\"><\/span>D. Check for Variable Using &#8220;echo&#8221; Command in Bash<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">It\u2019s a beginner-friendly method to verify the presence of a variable. However, if the variable<strong> contains<\/strong> values separated by spaces, this method is inappropriate here. You can check the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\na=&quot;Hello&quot;\nif [ -n &quot;$(echo $a)&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;variable exists.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable does not exist.&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>The <code class=\"\" data-line=\"\">-n &quot;$(printenv variable)&quot;<\/code> statement checks whether the length of the string obtained by echoing the value of the variable<code class=\"\" data-line=\"\"> $a<\/code> is non-zero.<\/div><\/div>\n<p>Now, run the script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25064\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/9.checking-variable-using-echo.png\" alt=\"checking variable using echo command\" width=\"372\" height=\"65\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/9.checking-variable-using-echo.png 372w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/9.checking-variable-using-echo-300x52.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/>You can see that the output shows the defined <strong>variable a exists<\/strong>.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"E_Using_the_%E2%80%9Cisset%E2%80%9D_Function_to_Check_If_a_Variable_is_Set\"><\/span>E. Using the &#8220;isset&#8221; Function to Check If a Variable is Set<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">The <strong>isset<\/strong> <strong>function<\/strong> checks the variable existence and if they are null. When it finds that the defined variable in the bash script and is not null, it returns the &#8220;<strong>True<\/strong>&#8221; as output. Check out the following script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nisset() {\n\u00a0\u00a0\u00a0 [ -n &quot;${!1}&quot; ]\n}\na=&quot;Hello&quot;\nif isset a; then\n\u00a0\u00a0\u00a0 echo &quot;variable is set.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable is unset.&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>In this script, the <code class=\"\" data-line=\"\">isset<\/code> function takes the name of the variable <code class=\"\" data-line=\"\">a<\/code> as an argument <code class=\"\" data-line=\"\">!1<\/code>. The <code class=\"\" data-line=\"\">$!1<\/code>\u00a0stores the value of the variable and using the -n option checks that if the variable is non-empty, then it will execute the code inside the <code class=\"\" data-line=\"\">if<\/code> block otherwise, it executes the <code class=\"\" data-line=\"\">else<\/code> block code. <\/div><\/div>\n<p>Now, run the script. <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25065\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/10.checking-variable-using-isset-function.png\" alt=\"checking variable using isset function\" width=\"390\" height=\"69\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/10.checking-variable-using-isset-function.png 390w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/10.checking-variable-using-isset-function-300x53.png 300w\" sizes=\"(max-width: 390px) 100vw, 390px\" \/> Since the <strong>a<\/strong>\u00a0variable is non-empty, the output shows that the variable is set.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"F_Check_for_Variable_Existence_Using_Parameter_Expansion\"><\/span>F. Check for Variable Existence Using Parameter Expansion<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-size: inherit; text-align: justify;\">You can employ the parameter expansion method with the <\/span><strong>-n<\/strong><span style=\"font-size: inherit; text-align: justify;\"><strong> option<\/strong> too. The <code class=\"\" data-line=\"\">${x+set}<\/code> inside the <strong>if<\/strong> condition replaces the variable <strong>x<\/strong> with <strong>set<\/strong> if<\/span><span style=\"font-size: inherit; text-align: justify;\"> the variable is set and not empty. The <code class=\"\" data-line=\"\">-n<\/code> option returns &#8220;<\/span><strong style=\"font-size: inherit; text-align: justify;\">true&#8221; <\/strong>when the variable is not empty<span style=\"font-size: inherit; text-align: justify;\">. Otherwise, the script will execute the <\/span><strong style=\"font-size: inherit; text-align: justify;\">&#8220;False&#8221;<\/strong><span style=\"font-size: inherit; text-align: justify;\"> code block. Now, you can follow the script given below:<\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nx=10\nif [[ -n ${x+set} ]];then\n\u00a0\u00a0\u00a0 echo &quot;variable x is set&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;variable x is not set&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>When you declare the variable <b>x <\/b>whether it is empty or not, it will execute the <strong>true if block<\/strong> code. On the other hand, it will display the <strong>else\u00a0block<\/strong> code.<\/div><\/div>\n<p>Now, run the script. <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25069\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/14.n-operator-with-set-modifier.png\" alt=\"checking variable using n option with set modifier\" width=\"391\" height=\"64\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/14.n-operator-with-set-modifier.png 391w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/14.n-operator-with-set-modifier-300x49.png 300w\" sizes=\"(max-width: 391px) 100vw, 391px\" \/> After running the script, the output shows that the &#8220;<strong>variable<\/strong> <strong>x is set&#8221;<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: inherit; text-align: justify;\">The same task can be done within the &#8220;<\/span>if else&#8221;<span style=\"font-size: inherit; text-align: justify;\"> statement with the <strong>-unset modifier<\/strong>. The <strong>-n operator<\/strong> with <strong>-unset modifier<\/strong> gives the output by checking the <\/span><strong style=\"font-size: inherit; text-align: justify;\">string length <\/strong><span style=\"font-size: inherit; text-align: justify;\">of the assigned variable. If the defined variable is null and unset, the variable value will be replaced with the <strong>unset<\/strong> modifier. Here, I have explained an example to check if the <\/span><strong style=\"font-size: inherit; text-align: justify;\">string variable<\/strong><span style=\"font-size: inherit; text-align: justify;\"> is empty or not set. Let\u2019s check it.<\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwebsite=&quot;Linuxsimply.com&quot;\nif [[ -n ${website-unset} ]];then\n\u00a0\u00a0\u00a0 echo &quot;String is set&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;String is not set&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>The <code class=\"\" data-line=\"\">-unset<\/code> modifier along with the <code class=\"\" data-line=\"\">-n<\/code> operator measures the string length and checks if the variable is empty or not. If the variable is empty, the output will execute as <strong>false<\/strong>. Here, &#8220;Linuxsimply.com&#8221; is the the string variable.<\/p>\n<\/div><\/div>\n<p>Now, run the script. <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25070\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/15.n-option-with-unset-modifier.png\" alt=\"checking variable using n option with unset modifier\" width=\"423\" height=\"64\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/15.n-option-with-unset-modifier.png 423w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/15.n-option-with-unset-modifier-300x45.png 300w\" sizes=\"(max-width: 423px) 100vw, 423px\" \/> Since I have set the <strong>Linuxsimply.com<\/strong> string in the bash script. Therefore, it shows <strong>&#8216;string is set&#8217;<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_%E2%80%9Cdeclare%E2%80%9D_Command\"><\/span>4. Using &#8220;declare&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To check if a variable exists in Bash, you can use the <a href=\"https:\/\/linuxsimply.com\/declare-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>declare<\/strong> <strong>command<\/strong><\/a> incorporating the <strong>-p<\/strong> <strong>option<\/strong>. You can check an example below to know more about it:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\na=&quot;hello&quot;\nif declare -p a &amp;&gt;\/dev\/null; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is set.&quot;\nelse\n\u00a0\u00a0\u00a0 echo &quot;The variable is unset.&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>Here, the <code class=\"\" data-line=\"\">declare<\/code> command with the help of <code class=\"\" data-line=\"\">-p<\/code> option checks the existence of a variable named <code class=\"\" data-line=\"\">a<\/code>. Whether the variable is empty or not, if it is set in the bash script, the script will return true. Otherwise, it will execute that the variable is not set. And <code class=\"\" data-line=\"\">&amp;&gt;\/dev\/null <\/code>is used to redirect both standard output and error to the &#8220;\/dev\/null&#8221; location. <\/div><\/div>\n<p>Now, run the script. <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25066\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/11.checking-variable-using-declare.png\" alt=\"checking variable using declare\" width=\"424\" height=\"66\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/11.checking-variable-using-declare.png 424w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/11.checking-variable-using-declare-300x47.png 300w\" sizes=\"(max-width: 424px) 100vw, 424px\" \/> The output shows that the variable is set as the variable <code class=\"\" data-line=\"\">a<\/code> defined in the script.<\/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;\">I hope this article helps you understand the whole<strong> procedure <\/strong>of how to check a variable&#8217;s existence. Here, I have described <strong>4 methods <\/strong>to verify the variable\u2019s existence. In addition, you can employ two modifiers of parameter expansion <strong>+set and -unset<\/strong> along with<strong> -z and -n<\/strong> options to check if a variable exists in bash. Read the article attentively and follow every step to finish your task <strong>successfully<\/strong>.<\/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=\"How_to_check_if_a_character_exists_in_a_string_in_bash\"><\/span><span style=\"font-size: inherit;\">How to check if a character exists in a string in bash?<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-size: inherit;\">You can check it with different methods such as pattern matching or looping through the characters. The common way is to use the<\/span><code class=\"\" data-line=\"\"> grep<\/code><span style=\"font-size: inherit;\"> command within the <\/span><strong style=\"font-size: inherit;\">if else <\/strong><span style=\"font-size: inherit;\">statement. Here is an example script that checks whether &#8216;o&#8217; exist in &#8216;Hello, World&#8217;<\/span><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Hello, world!&quot;\nchar=&quot;o&quot;\n\nif grep -q &quot;$char&quot; &lt;&lt;&lt; &quot;$string&quot;; then\necho &quot;Character &#039;$char&#039; exists in the string.&quot;\nelse\necho &quot;Character &#039;$char&#039; does not exist in the string.&quot;\nfi<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"What_is_%E2%80%98_in_bash\"><\/span>What is \u2018${}\u2019 in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In Bash, the ${} notation is used for parameter expansion, allowing you to access the value of a variable. When you enclose a variable name within ${}, Bash replaces it with the value assigned to that variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_read_a_variable_in_bash\"><\/span>How to read a variable in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To read a variable, use the <strong>read<\/strong> <strong>command<\/strong> followed by the variable name. The basic syntax is: <strong>read variable_name<\/strong><br \/>\nThis command prompts the user to input a value, and whatever the user enters is stored in the specified variable.<br \/>\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"How to check if a character exists in a string in bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"You can check it with different methods such as pattern matching or looping through the characters. The common way is to use the grep command within the if else statement. Here is an example script that checks whether 'o' exist in 'Hello, World'\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is \u2018${}\u2019 in bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In Bash, the ${} notation is used for parameter expansion, allowing you to access the value of a variable. When you enclose a variable name within ${}, Bash replaces it with the value assigned to that variable.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to read a variable in bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In Bash, reading a variable involves using the read command followed by the variable name. The basic syntax is: read variable_name\nThis command prompts the user to input a value, and whatever the user enters is stored in the specified variable.\"\n    }\n  }]\n}\n<\/script><br \/>\n<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-else\/if-argument-exists\/\" target=\"_blank\" rel=\"noopener\">Check If An Input Argument Exists in Bash [4 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-contains-string\/\" target=\"_blank\" rel=\"noopener\">Check If a String Contains Substring, Number &amp; Letter in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-string-not-empty\/\" target=\"_blank\" rel=\"noopener\">Check If a String is Empty or Not in Bash [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/compare-strings\/\" target=\"_blank\" rel=\"noopener\">How to Compare Strings in Bash With if Statement [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/compare-numbers\/\" target=\"_blank\" rel=\"noopener\">How to Compare Numbers in Bash With If Statement [2 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-in-array\/\" target=\"_blank\" rel=\"noopener\">Check If Array Contains an Element in Bash [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-else-one-line\/\" target=\"_blank\" rel=\"noopener\">Bash if-else Condition in One-Line [2 Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/grep\/\" target=\"_blank\" rel=\"noopener\">Using grep in if Statement in Bash [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/regex\/\" target=\"_blank\" rel=\"noopener\">How to Use Regex in Bash If Condition? [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/rm-if-exists\/\" target=\"_blank\" rel=\"noopener\">Bash rm If Exists [All Case Explained]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/dollar-sign\/\" target=\"_blank\" rel=\"noopener\">What is \u2018$?\u2019 in the Bash if Statement? [8 Examples]<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;If Else in Bash&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\/if-else\/\"><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><\/span>\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\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;25051&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;5&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 - (5 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Check If a Variable Exists in Bash Using If Statement&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 - (5 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Bash, the &#8220;if else&#8221; statement is used to control the flow of execution of the bash script statements. Within &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Check If a Variable Exists in Bash Using If Statement\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#more-25051\" aria-label=\"Read more about Check If a Variable Exists in Bash Using If Statement\">Read more<\/a><\/p>\n","protected":false},"author":314908,"featured_media":25071,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[167],"class_list":["post-25051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-if-else-statement","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>Check If a Variable Exists in Bash Using If Statement - LinuxSimply<\/title>\n<meta name=\"description\" content=\"This article contains different methods of checking if a variable exists in bash. Multiple options and modifiers used to check variable.\" \/>\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\/if-else\/if-variable-exists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Check If a Variable Exists in Bash Using If Statement - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"This article contains different methods of checking if a variable exists in bash. Multiple options and modifiers used to check variable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/\" \/>\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-10-17T08:31:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-23T06:12:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/check-if-a-variable-exists.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=\"15 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\\\/if-else\\\/if-variable-exists\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/\"},\"author\":{\"name\":\"Mitu Akter Mou\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\"},\"headline\":\"Check If a Variable Exists in Bash Using If Statement\",\"datePublished\":\"2023-10-17T08:31:27+00:00\",\"dateModified\":\"2024-01-23T06:12:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/\"},\"wordCount\":2624,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/check-if-a-variable-exists.png\",\"keywords\":[\"if else statement\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/\",\"name\":\"Check If a Variable Exists in Bash Using If Statement - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/check-if-a-variable-exists.png\",\"datePublished\":\"2023-10-17T08:31:27+00:00\",\"dateModified\":\"2024-01-23T06:12:18+00:00\",\"description\":\"This article contains different methods of checking if a variable exists in bash. Multiple options and modifiers used to check variable.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/check-if-a-variable-exists.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/check-if-a-variable-exists.png\",\"width\":400,\"height\":400,\"caption\":\"explaining how to check if a variable exists in bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/if-variable-exists\\\/#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\":\"If Else in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if-else\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Check If a Variable Exists in Bash Using If Statement\"}]},{\"@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":"Check If a Variable Exists in Bash Using If Statement - LinuxSimply","description":"This article contains different methods of checking if a variable exists in bash. Multiple options and modifiers used to check variable.","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\/if-else\/if-variable-exists\/","og_locale":"en_US","og_type":"article","og_title":"Check If a Variable Exists in Bash Using If Statement - LinuxSimply","og_description":"This article contains different methods of checking if a variable exists in bash. Multiple options and modifiers used to check variable.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/mitalimostakim.mou\/","article_published_time":"2023-10-17T08:31:27+00:00","article_modified_time":"2024-01-23T06:12:18+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/check-if-a-variable-exists.png","type":"image\/png"}],"author":"Mitu Akter Mou","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mitu Akter Mou","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/"},"author":{"name":"Mitu Akter Mou","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689"},"headline":"Check If a Variable Exists in Bash Using If Statement","datePublished":"2023-10-17T08:31:27+00:00","dateModified":"2024-01-23T06:12:18+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/"},"wordCount":2624,"commentCount":2,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/check-if-a-variable-exists.png","keywords":["if else statement"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/","name":"Check If a Variable Exists in Bash Using If Statement - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/check-if-a-variable-exists.png","datePublished":"2023-10-17T08:31:27+00:00","dateModified":"2024-01-23T06:12:18+00:00","description":"This article contains different methods of checking if a variable exists in bash. Multiple options and modifiers used to check variable.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/check-if-a-variable-exists.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/check-if-a-variable-exists.png","width":400,"height":400,"caption":"explaining how to check if a variable exists in bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/if-variable-exists\/#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":"If Else in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if-else\/"},{"@type":"ListItem","position":5,"name":"Check If a Variable Exists in Bash Using If Statement"}]},{"@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\/25051","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=25051"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/25051\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/25071"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=25051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=25051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=25051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}