{"id":27401,"date":"2023-11-19T05:11:18","date_gmt":"2023-11-19T05:11:18","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=27401"},"modified":"2024-05-09T11:34:52","modified_gmt":"2024-05-09T11:34:52","slug":"bash-if-empty","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/","title":{"rendered":"How to Check If a Variable is Empty\/Null in Bash? [4 Methods]"},"content":{"rendered":"<p style=\"text-align: justify;\">To check if a variable is empty or null in Bash, use <strong>-z, -n, equal comparison<\/strong> with if statement. In this article, I will demonstrate 4 methods to check if a variable is empty\/null or not in Bash utilizing different conditional operators.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Practice-Files_Bash-Check-If-Variable-Is-Empty-Or-Not.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 is Empty or Not 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\/if-empty-or-not\/#What_is_an_EmptyNull_Variable_in_Bash\" >What is an Empty\/Null Variable in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#4_Ways_to_Check_If_a_Variable_is_Empty_or_Not_in_Bash\" >4 Ways to Check If a Variable is Empty or Not 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-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#1_Using_%E2%80%98-z_Option\" >1. Using &#8216;-z&#8217; Option<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#2_Using_%E2%80%9C-n%E2%80%9D_Option\" >2. Using \u201c-n\u201d Option<\/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\/if-empty-or-not\/#3_Using_Parameter_Expansion\" >3. Using Parameter Expansion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#4_Using_Equal_Operator\" >4. Using Equal Operator<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#How_to_Check_if_a_Variable_is_Not_Empty_in_Bash\" >How to Check if a Variable is Not Empty in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#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-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#How_can_I_check_if_a_bash_variable_is_null\" >How can I check if a bash variable is null?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#Why_check_variable_emptiness_in_Bash\" >Why check variable emptiness in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#Whats_the_difference_between_checking_if_a_variable_is_unset_and_checking_if_its_empty\" >What&#8217;s the difference between checking if a variable is unset and checking if it&#8217;s empty?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#What_happens_when_a_variable_is_kept_empty_in_Bash\" >What happens when a variable is kept empty in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#Can_I_use_a_single_%E2%80%98if_statement_to_check_for_both_empty_and_non-empty_variables\" >Can I use a single &#8216;if&#8217; statement to check for both empty and non-empty variables?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_an_EmptyNull_Variable_in_Bash\"><\/span>What is an Empty\/Null Variable in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">An empty variable is a variable that has been declared but contains no value or has a value that. This means that the variable has been assigned, but it holds a null value. In a word, if a variable contains no value in it, then it&#8217;s called an empty or null variable. Typically, an empty variable appears for <strong>two<\/strong> reasons:<\/p>\n<ol>\n<li>The variable was not initialized.<\/li>\n<li>The variable was assigned a value at some point, but the value was later cleared.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"4_Ways_to_Check_If_a_Variable_is_Empty_or_Not_in_Bash\"><\/span>4 Ways to Check If a Variable is Empty or Not in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Bash provides several conditional tasks to check whether a variable is empty or not, including variable length check, parameter expansion check, double square bracket check, equal operator check, negation operator check, etc. Let\u2019s explore them.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_%E2%80%98-z_Option\"><\/span>1. Using &#8216;-z&#8217; Option<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The \u201c<strong>-z<\/strong>\u201d option in \u2018if\u2019 statement checks if a string contains zero length, i.e. determines if the assigned variable is empty. If the specified variable length is zero, a <strong>True <\/strong>expression is executed.<\/p>\n<p style=\"text-align: justify;\">To check if a variable is empty or null, use the <strong>-z <\/strong>option with the <strong>test <\/strong>command either in the format <code class=\"\" data-line=\"\">if [ -z &quot;$variable&quot; ]<\/code> or <code class=\"\" data-line=\"\">if test -z &quot;$variable&quot;<\/code>. Let\u2019s take a look at the following example using the later one:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining an empty string variable\nxyz=&quot;&quot;\n\n#Checking if variable is empty\nif test -z &quot;$xyz&quot;; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is 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\">\n<p style=\"text-align: justify;\">In the script, the &#8216;if&#8217; conditional checks whether the defined variable \u2018xyz\u2019 is empty. If the variable is empty, it echoes &#8220;The variable is empty.&#8221; employing the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a>.<\/p>\n<\/div><\/div><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27415\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img1-Using-z-option-with-test-command-for-empty-variable-validation-1.png\" alt=\"Using '-z' option with 'test' command for empty variable validation\" width=\"634\" height=\"180\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img1-Using-z-option-with-test-command-for-empty-variable-validation-1.png 634w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img1-Using-z-option-with-test-command-for-empty-variable-validation-1-300x85.png 300w\" sizes=\"(max-width: 634px) 100vw, 634px\" \/>As you can see from the figure above, the variable &#8216;xyz&#8217; contains a string with zero (0) length. This means that the &#8216;xyz&#8217; variable is empty.<\/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;\">Whenever using <strong>if<\/strong> statement with <strong>[ ]<\/strong>, remember to add spaces between the brackets and text inside it.<\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_%E2%80%9C-n%E2%80%9D_Option\"><\/span>2. Using \u201c-n\u201d Option<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In the &#8216;if&#8217; statement, the <code class=\"\" data-line=\"\">-n<\/code> option evaluates whether a variable has a value with non-zero length i.e. the variable is not empty. If the variable contains any value, then the test returns true. However, you can use the <code class=\"\" data-line=\"\">-n<\/code> parameter to check whether a variable that contains a number is not empty. In this case, Bash treats numbers as strings.<\/p>\n<p style=\"text-align: justify;\">For instance, you can do the following non-empty variable testing using the <code class=\"\" data-line=\"\">if [ -n &quot;$number&quot; ]<\/code> syntax like the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining a variable\nnumber=41\n\n#Checking if variable is not empty using \u201c-n\u201d option\nif [ -n &quot;$number&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is 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\">\n<p style=\"text-align: justify;\">Here, the <code class=\"\" data-line=\"\">-n<\/code> option checks if the defined variable \u2018number\u2019 has a value. Since the variable has value 41, the condition evaluates to true and the script prints &#8220;The variable is not empty.&#8221;<\/p>\n<p style=\"text-align: justify;\"><\/div><\/div><img decoding=\"async\" class=\"aligncenter size-full wp-image-27416\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img2-Using-n-option-for-non-empty-variable-validation-1.png\" alt=\"Using '-n' option with '[ ]' construct for non-empty variable validation\" width=\"700\" height=\"212\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img2-Using-n-option-for-non-empty-variable-validation-1.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img2-Using-n-option-for-non-empty-variable-validation-1-300x91.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>In the figure above, you can see that the variable \u2018number\u2019 has a string value of \u201841\u2019. Therefore, the string length of the variable is non-zero. This indicates that the variable \u2018number\u2019 is not empty.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_Parameter_Expansion\"><\/span>3. Using Parameter Expansion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Parameter expansion is a strong feature in Bash that is used for a brief validation of variables i.e. whether it is empty or not. The syntax for parameter expansion practiced in empty variable validation is <code class=\"\" data-line=\"\">${variable-}<\/code>.<\/p>\n<p style=\"text-align: justify;\">To find out if a variable is empty or not, use the <code class=\"\" data-line=\"\">if [ -z &quot;${variable-}&quot; ]<\/code> &amp; <code class=\"\" data-line=\"\">if [ -n &quot;${variable-}&quot; ]<\/code> syntaxes to execute the output when it becomes true. For example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining an empty string variable\nxyz=&quot;&quot;\nabc=&#039;Hello, Linux!&#039;\n\n#Using parameter expansion to check if the variable is empty\nif [ -z &quot;${xyz-}&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is empty.&quot;\nfi\n#Using parameter expansion to check if the variable is not empty\nif [ -n &quot;${abc-}&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is 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\">\n<p style=\"text-align: justify;\">Since two variables are set such as one as an empty string and another with some value in this case, the parameter expansions <code class=\"\" data-line=\"\">${xyz-}<\/code> and <code class=\"\" data-line=\"\">${abc-}<\/code> evaluate to <code class=\"\" data-line=\"\">$xyz<\/code> and <code class=\"\" data-line=\"\">$abc<\/code> and the script prints &#8220;The variable is empty.&#8221; and &#8220;The variable is not empty.&#8221; respectively resulting from the conditional check.<\/p>\n<p style=\"text-align: justify;\"><\/div><\/div><img decoding=\"async\" class=\"aligncenter size-full wp-image-27417\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img3-Using-parameter-expansion-for-empty-non-empty-variable-validation-1.png\" alt=\"Using parameter expansion for empty &amp; non-empty variable validation\" width=\"700\" height=\"188\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img3-Using-parameter-expansion-for-empty-non-empty-variable-validation-1.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img3-Using-parameter-expansion-for-empty-non-empty-variable-validation-1-300x81.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>In the above snapshot, the variable \u2018xyz\u2019 has a string length of zero (0), which is an empty variable, whereas the variable \u2018abc\u2019 holds the value \u2018Hello, Linux!\u2019, which is a non-empty variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_Equal_Operator\"><\/span>4. Using Equal Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The \u201c<strong>==<\/strong>\u201d operator is generally used for string comparison, pattern matching, etc. However, this operator can be used within single square brackets \u201c<strong>[ ]<\/strong>\u201d to check if a variable is empty by comparing it with an empty string.<\/p>\n<p style=\"text-align: justify;\">Here\u2019s an example of how you can use <code class=\"\" data-line=\"\">if [ &quot;$variable&quot; = &quot;&quot; ]<\/code> syntax to check if a variable is empty:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining an empty variable\nxyz=&quot;&quot;\nif [ &quot;$xyz&quot; == &quot;&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is 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\">\n<p style=\"text-align: justify;\">In the above code, the condition inside the \u201c<strong>[ ]<\/strong>\u201d operator checks if the specified variable &#8216;xyz&#8217; is equal to an empty string. If the variable is empty, then the condition becomes true and the script displays &#8220;The variable is empty.&#8221;<\/p>\n<p style=\"text-align: justify;\"><\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-27419\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img5-Using-operator-for-empty-variable-validation-1.png\" alt=\"Using '=' operator for empty variable validation\" width=\"700\" height=\"169\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img5-Using-operator-for-empty-variable-validation-1.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img5-Using-operator-for-empty-variable-validation-1-300x72.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>When the variable \u2018xyz\u2019 is found as an empty string in \u2018if\u2019, it is treated as an empty variable as shown in the figure above.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Check_if_a_Variable_is_Not_Empty_in_Bash\"><\/span>How to Check if a Variable is Not Empty in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The \u201c<strong>!<\/strong>\u201d operator in Bash is used to reverse the output of a condition. This operator along with the equal \u201c<strong>=<\/strong>\u201d operator can be used within single square brackets \u201c<strong>[ ]<\/strong>\u201d for empty variable validation.<\/p>\n<p style=\"text-align: justify;\">To check if a variable is empty or not, use <code class=\"\" data-line=\"\">if [ &quot;$variable&quot; != &quot;&quot; ]<\/code> syntax in the following manner:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining a variable\nname=&quot;N&quot;\n\n#Checking if variable is not empty using \u201c!=\u201d operator\nif [ &quot;$name&quot; != &quot;&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is 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\">\n<p style=\"text-align: justify;\">The \u2018if\u2019 condition here compares the specified \u2018name\u2019 variable with an empty string and checks if the variable is not equal to the empty string. As the variable contains a value, the condition becomes true and the script shows &#8220;The variable is not empty.&#8221;<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-27420\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img6-Using-operator-with-for-non-empty-variable-validation-1.png\" alt=\"Using '!' operator with '=' for non-empty variable validation\" width=\"692\" height=\"208\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img6-Using-operator-with-for-non-empty-variable-validation-1.png 692w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/img6-Using-operator-with-for-non-empty-variable-validation-1-300x90.png 300w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/>From the above figure, you can see that the variable &#8216;name&#8217; is treated as a non-empty variable when the value <strong>N<\/strong>\u00a0is specified in &#8216;if&#8217;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In summary, this article provides you with several options to check whether a variable is empty or not in Bash. You can choose any method according to your preference and use this as a best practice for writing flexible Bash scripts.<\/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_can_I_check_if_a_bash_variable_is_null\"><\/span>How can I check if a bash variable is null?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To check if a Bash variable is null\/empty, use <strong>-z<\/strong> option with <strong>if<\/strong> statement. Here&#8217;s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">if [ -z &quot;$variable&quot; ]; then\n  echo &quot;Variable is empty.&quot;\nelse\n  echo &quot;Variable is not empty.&quot;\nfi<\/code><\/pre>\n<p>This script will determine whether <code class=\"\" data-line=\"\">$variable<\/code> is null\/empty or not.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Why_check_variable_emptiness_in_Bash\"><\/span>Why check variable emptiness in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Checking variable emptiness in Bash is crucial for several reasons:<\/p>\n<ol>\n<li>To ensure proper handling and validation of user input.<\/li>\n<li>To prevent unintended errors.<\/li>\n<li>To maintain data integrity.<\/li>\n<li>To control the conditional decisions.<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"Whats_the_difference_between_checking_if_a_variable_is_unset_and_checking_if_its_empty\"><\/span>What&#8217;s the difference between checking if a variable is unset and checking if it&#8217;s empty?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Checking if a variable is unset confirms whether the variable has been defined or assigned any value. The \u2018-v\u2019 operator checks if a variable is set using the syntax <code class=\"\" data-line=\"\">if [ -v variable ]<\/code>. On the contrary, checking if a variable is empty ensures that the variable contains an empty string. The \u2018-z\u2019 operator validates this check using the <code class=\"\" data-line=\"\">if [ -z &quot;$variable&quot; ]<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_happens_when_a_variable_is_kept_empty_in_Bash\"><\/span>What happens when a variable is kept empty in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">When a variable is empty in Bash, it means that the variable has no value assigned to it. In such cases, the variable is considered to be null or empty. So, keeping a variable empty may lead to unwanted crash of a program. Also, it leads to unnecessary wastage of memory.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_use_a_single_%E2%80%98if_statement_to_check_for_both_empty_and_non-empty_variables\"><\/span>Can I use a single &#8216;if&#8217; statement to check for both empty and non-empty variables?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, to check for both empty and non-empty variables within the same &#8216;<strong>if<\/strong>&#8216; statement, you can use the <code class=\"\" data-line=\"\">-z<\/code> and <code class=\"\" data-line=\"\">-n<\/code> options. For instance:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Defining a variable\nvariable=&quot;&quot;\n\n#Checking for empty &amp; non-empty variables\nif [ -z &quot;$variable&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is empty.&quot;\nelif [ -n &quot;$variable&quot; ]; then\n\u00a0\u00a0\u00a0 echo &quot;The variable is not empty.&quot;\nfi<\/code><\/pre>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-options\/\" target=\"_blank\" rel=\"noopener\">Mastering 10 Essential Options of If Statement in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-true-false\/\" target=\"_blank\" rel=\"noopener\">How to Check a Boolean If True or False in Bash [Easy Guide]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-test\/\" target=\"_blank\" rel=\"noopener\">Bash Test Operations in \u2018If\u2019 Statement<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/set-variable-if-not\/\" target=\"_blank\" rel=\"noopener\">Check If a Variable is Set or Not 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\/environment-variable-exists\/\" target=\"_blank\" rel=\"noopener\">Check If Environment Variable Exists 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\/modulo-if\/\" target=\"_blank\" rel=\"noopener\">Bash Modulo Operations in &#8220;If&#8221; Statement [4 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/or-and-not-in-if\/\" target=\"_blank\" rel=\"noopener\">How to Use &#8220;OR&#8221;, &#8220;AND&#8221;, &#8220;NOT&#8221; in Bash If Statement [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/multiple-if-conditions\/\" target=\"_blank\" rel=\"noopener\">Evaluate Multiple Conditions in Bash \u201cIf\u201d Statement [2 Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/double-brackets\/\" target=\"_blank\" rel=\"noopener\">Using Double Square Brackets \u201c[[ ]]\u201d in If Statement in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/check-if-file-exists\/\" target=\"_blank\" rel=\"noopener\">6 Ways to Check If a File Exists or Not in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/check-if-file-is-empty\/\" target=\"_blank\" rel=\"noopener\">How to Check If a File is Empty 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\/check-if-directory-exists\/\" target=\"_blank\" rel=\"noopener\">7 Ways to Check If Directory Exists or Not in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/negate-if\/\" target=\"_blank\" rel=\"noopener\">Negate an &#8220;If&#8221; Condition in Bash [4 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-command-fails\/\" target=\"_blank\" rel=\"noopener\">Check If Bash Command Fail or Succeed [Exit If Fail]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-one-line\/\" target=\"_blank\" rel=\"noopener\">How to Write If Statement in One Line? [2 Easy Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-loop\/\" target=\"_blank\" rel=\"noopener\">Different Loops with If Statements in Bash [5 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/flags\/\" target=\"_blank\" rel=\"noopener\">How to Use Flags in Bash If Condition? [With Example]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/compare-dates\/\" target=\"_blank\" rel=\"noopener\">Learn to Compare Dates in Bash [4 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 Statement 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\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/\" target=\"_blank\" rel=\"noopener\">If Statement in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Conditional Statements&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/\" target=\"_blank\" rel=\"noopener\">Bash Conditional Statements<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/strong><\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": {\n    \"@type\": \"Question\",\n    \"name\": \"What happens when a variable is kept empty in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"When a variable is empty in Bash, it means that the variable has no value assigned to it. In such cases, the variable is considered to be null or empty. So, keeping a variable empty may lead to unwanted crash of a program. Also, it leads to unnecessary wastage of memory.\"\n    }\n  }\n}\n<\/script><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;27401&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;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&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;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Check If a Variable is Empty\\\/Null in Bash? [4 Methods]&quot;,&quot;width&quot;:&quot;0&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: 0px;\">\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            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>To check if a variable is empty or null in Bash, use -z, -n, equal comparison with if statement. In &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Check If a Variable is Empty\/Null in Bash? [4 Methods]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#more-27401\" aria-label=\"Read more about How to Check If a Variable is Empty\/Null in Bash? [4 Methods]\">Read more<\/a><\/p>\n","protected":false},"author":314905,"featured_media":27422,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[166],"class_list":["post-27401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-if-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>How to Check If a Variable is Empty\/Null in Bash? [4 Methods] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Choose a suitable method according to your script&#039;s requirements to check if a variable is empty or not in Bash.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Check If a Variable is Empty\/Null in Bash? [4 Methods] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Choose a suitable method according to your script&#039;s requirements to check if a variable is empty or not in Bash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-19T05:11:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-09T11:34:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.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=\"Nadiba Rahman\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nadiba Rahman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 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\\\/if-empty-or-not\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/\"},\"author\":{\"name\":\"Nadiba Rahman\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/69b717a03a5d9dc48e683aaffe22d33f\"},\"headline\":\"How to Check If a Variable is Empty\\\/Null in Bash? [4 Methods]\",\"datePublished\":\"2023-11-19T05:11:18+00:00\",\"dateModified\":\"2024-05-09T11:34:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/\"},\"wordCount\":1467,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png\",\"keywords\":[\"if statement\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/\",\"name\":\"How to Check If a Variable is Empty\\\/Null in Bash? [4 Methods] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png\",\"datePublished\":\"2023-11-19T05:11:18+00:00\",\"dateModified\":\"2024-05-09T11:34:52+00:00\",\"description\":\"Choose a suitable method according to your script's requirements to check if a variable is empty or not in Bash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png\",\"width\":400,\"height\":400,\"caption\":\"Feature image-Bash check if a variable is empty or not\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/if-empty-or-not\\\/#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 Statement in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/conditional-statements\\\/if\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Check If a Variable is Empty\\\/Null in Bash? [4 Methods]\"}]},{\"@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\\\/69b717a03a5d9dc48e683aaffe22d33f\",\"name\":\"Nadiba Rahman\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Nadiba-Rahman-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Nadiba-Rahman-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Nadiba-Rahman-II-96x96.png\",\"caption\":\"Nadiba Rahman\"},\"description\":\"Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor\u2019s degree in Electronics &amp; Telecommunication Engineering from Rajshahi University of Engineering &amp; Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/nadiba.rahman?mibextid=ZbWKwL\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/nadiba-rahman-443459171\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/nadiba\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Check If a Variable is Empty\/Null in Bash? [4 Methods] - LinuxSimply","description":"Choose a suitable method according to your script's requirements to check if a variable is empty or not in Bash.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/","og_locale":"en_US","og_type":"article","og_title":"How to Check If a Variable is Empty\/Null in Bash? [4 Methods] - LinuxSimply","og_description":"Choose a suitable method according to your script's requirements to check if a variable is empty or not in Bash.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL","article_published_time":"2023-11-19T05:11:18+00:00","article_modified_time":"2024-05-09T11:34:52+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png","type":"image\/png"}],"author":"Nadiba Rahman","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nadiba Rahman","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/"},"author":{"name":"Nadiba Rahman","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/69b717a03a5d9dc48e683aaffe22d33f"},"headline":"How to Check If a Variable is Empty\/Null in Bash? [4 Methods]","datePublished":"2023-11-19T05:11:18+00:00","dateModified":"2024-05-09T11:34:52+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/"},"wordCount":1467,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png","keywords":["if statement"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/","name":"How to Check If a Variable is Empty\/Null in Bash? [4 Methods] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png","datePublished":"2023-11-19T05:11:18+00:00","dateModified":"2024-05-09T11:34:52+00:00","description":"Choose a suitable method according to your script's requirements to check if a variable is empty or not in Bash.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/Feature-image-Bash-check-if-a-variable-is-empty-or-not.png","width":400,"height":400,"caption":"Feature image-Bash check if a variable is empty or not"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/if-empty-or-not\/#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 Statement in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/conditional-statements\/if\/"},{"@type":"ListItem","position":5,"name":"How to Check If a Variable is Empty\/Null in Bash? [4 Methods]"}]},{"@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\/69b717a03a5d9dc48e683aaffe22d33f","name":"Nadiba Rahman","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Nadiba-Rahman-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Nadiba-Rahman-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Nadiba-Rahman-II-96x96.png","caption":"Nadiba Rahman"},"description":"Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor\u2019s degree in Electronics &amp; Telecommunication Engineering from Rajshahi University of Engineering &amp; Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/nadiba.rahman?mibextid=ZbWKwL","https:\/\/www.linkedin.com\/in\/nadiba-rahman-443459171"],"url":"https:\/\/linuxsimply.com\/author\/nadiba\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/27401","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\/314905"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=27401"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/27401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/27422"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=27401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=27401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=27401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}