{"id":417928,"date":"2022-03-17T16:00:38","date_gmt":"2022-03-17T13:00:38","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/"},"modified":"2022-03-17T16:00:38","modified_gmt":"2022-03-17T13:00:38","slug":"how-to-use-set-and-pipefail-in-bash-scripts-on-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/","title":{"rendered":"#How To Use set and pipefail in Bash Scripts on Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a25e8b2f22bc\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" 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: #dd3333;color:#dd3333\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a25e8b2f22bc\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#%E2%80%9CHow_To_Use_set_and_pipefail_in_Bash_Scripts_on_Linux%E2%80%9D\" >&#8220;How To Use set and pipefail in Bash Scripts on Linux&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#Bash_Scripts_and_Error_Conditions\" >Bash Scripts and Error Conditions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#Demonstrating_the_Problem\" >Demonstrating the Problem<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#The_set_-e_Option\" >The set -e Option<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#Dealing_With_Failures_in_Pipes\" >Dealing With Failures in Pipes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#Catching_Uninitialized_Variables\" >Catching Uninitialized Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/#Sealed_With_a_x\" >Sealed With a x<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_To_Use_set_and_pipefail_in_Bash_Scripts_on_Linux%E2%80%9D\"><\/span>&#8220;How To Use set and pipefail in Bash Scripts on Linux&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-770699\" data-pagespeed-lazy-srcset=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Linux terminal on a laptop screen over a blue background.\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/command-line-interface-cli-programming-language-1278851809\">fatmawati achmad zaenuri\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>The Linux <code>set<\/code> and <code>pipefail<\/code> commands dictate what happens when a failure occurs in a Bash <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>. There\u2019s more to think about than should it stop or should it carry on.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>The Beginner&#8217;s Guide to Shell Scripting: The Basics<\/em><\/strong><\/p>\n<h2 id=\"bash-scripts-and-error-conditions\"><span class=\"ez-toc-section\" id=\"Bash_Scripts_and_Error_Conditions\"><\/span>Bash Scripts and Error Conditions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Bash shell scripts are great. They\u2019re quick to write and they don\u2019t need compiling. Any repetitive or multi-stage action that you need to perform can be wrapped in a convenient script. And because scripts can call any of the standard Linux utilities, you\u2019re not limited to the capabilities of the shell language itself.<\/p>\n<p>But problems can arise when you call an external utility or program. If it fails, the external utility will close down and send a return code to the shell, and it might even print an error message to the terminal. But your script will carry on processing. Perhaps that\u2019s not what you wanted. If an error occurs early in the execution of the script it might lead to worse issues if the rest of the script is allowed to run.<\/p>\n<p>You could check the return code from each external process as they complete, but that becomes difficult when processes are piped into other processes. The return code will be from the process at the end of the pipe, not the one in the middle that failed. Of course, errors can occur inside your script too, such as trying to access an uninitialized variable.<\/p>\n<p>The <code>set<\/code> and <code>pipefile<\/code> commands let you decide what happens when errors like these occur. They also let you detect errors even when they happen in the middle of a pipe chain.<\/p>\n<p>Here\u2019s how to use them.<\/p>\n<h2 id=\"demonstrating-the-problem\"><span class=\"ez-toc-section\" id=\"Demonstrating_the_Problem\"><\/span>Demonstrating the Problem<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s a trivial Bash script. It echoes two lines of text to the terminal. You can run this script if you copy the text into an editor and save it as \u201cscript-1.sh.\u201d<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho This will happen first &#13;\necho This will happen second<\/pre>\n<p>To make it executable you\u2019ll need to use <code>chmod<\/code>:<\/p>\n<pre>chmod +x script-1.sh<\/pre>\n<p>You\u2019ll need to run that command on each script if you want to run them on your computer. Let\u2019s run the script:<\/p>\n<pre>.\/script-1.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782538\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/1-8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a simple script with no errors.\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The two lines of text are sent to the terminal window as expected.<\/p>\n<p>Let\u2019s modify the script slightly. We\u2019ll ask <code>ls<\/code> to list the details of a file that doesn\u2019t exist. This will fail. We saved this as \u201cscript-2.sh\u201d and made it executable.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho This will happen first&#13;\nls imaginary-filename&#13;\necho This will happen second<\/pre>\n<p>When we run this script we see the error message from <code>ls<\/code> .<\/p>\n<pre>.\/script-2.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782539\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/2-8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a script and generating a fail condition.\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Although the <code>ls<\/code> command failed, the script continued to run. And even though there was an error during the script\u2019s execution, the return code from the script to the shell is zero, which indicates success. We can check this using echo and the <code>$?<\/code> variable which holds the last return code sent to the shell.<\/p>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782540\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/3-10.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Checking the return code for the last script executed.\" width=\"645\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The zero that gets reported is the return code from the second echo in the script. So there are two issues with this scenario. The first is the script had an error but it carried on running. That can lead to other problems if the rest of the script expects or depends on the action that failed actually succeeded. And the second is that if another script or process needs to check the success or failure of this script, it\u2019ll get a false reading.<\/p>\n<h2 id=\"the-set--e-option\"><span class=\"ez-toc-section\" id=\"The_set_-e_Option\"><\/span>The set -e Option<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>set -e<\/code> (exit) option causes a script to exit if any of the processes it calls generate a non-zero return code. Anything non-zero is taken to be a failure.<\/p>\n<p>By adding the <code>set -e<\/code> option to the start of the script, we can change its behavior. This is \u201cscript-3.sh.\u201d<\/p>\n<pre>#!\/bin\/bash &#13;\nset -e&#13;\n&#13;\necho This will happen first&#13;\nls imaginary-filename&#13;\necho This will happen second<\/pre>\n<p>If we run this script we\u2019ll see the effect of <code>set -e<\/code>.<\/p>\n<pre>.\/script-3.sh<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782544\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/4-8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Terminating a script upon an error condition, and correctly setting the return code.\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The script is halted and the return code sent to the shell is a non-zero value.<\/p>\n<h2 id=\"dealing-with-failures-in-pipes\"><span class=\"ez-toc-section\" id=\"Dealing_With_Failures_in_Pipes\"><\/span>Dealing With Failures in Pipes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Piping adds more complexity to the problem. The return code that comes out of a piped sequence of commands is the return code from the last command in the chain. If there\u2019s a failure with a command in the middle of the chain we\u2019re back to square one. That return code is lost, and the script will carry on processing.<\/p>\n<p>We can see the effects of piping commands with different return codes using the <code>true<\/code> and <code>false<\/code> shell built-ins. These two commands do no more than generate a return code of zero or one, respectively.<\/p>\n<pre>true<\/pre>\n<pre>echo $?<\/pre>\n<pre>false<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782543\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/5-8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The bash shell true and false built-in commands.\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we pipe <code>false<\/code> into <code>true<\/code> \u2014with <code>false<\/code> representing a failing process\u2014we get <code>true<\/code>\u2018s return code of zero.<\/p>\n<pre>false | true<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782545\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/6-7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Piping false into true.\" width=\"645\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Bash does have an array variable called <code>PIPESTATUS<\/code>, and this captures all of the return codes from each program in the pipe chain.<\/p>\n<pre>false | true | false | true<\/pre>\n<pre>echo \"${PIPESTATUS[0]} ${PIPESTATUS[1]} ${PIPESTATUS[2]} ${PIPESTATUS[3]}\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782546\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/7-7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using PIPESTATUS to see the return code of all programs in a pipe chain.\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><code>PIPESTATUS<\/code> only holds the return codes until the next program runs, and trying to determine which return code goes with which program can get very messy very quickly.<\/p>\n<p>This is where <code>set -o<\/code> (options) and <code>pipefail<\/code> come in. This is \u201cscript-4.sh.\u201d This will try to pipe the contents of a file that doesn\u2019t exist into <code>wc<\/code>.<\/p>\n<pre>#!\/bin\/bash &#13;\nset -e&#13;\n&#13;\necho This will happen first&#13;\ncat script-99.sh | wc -l&#13;\necho This will happen second<\/pre>\n<p>This fails, as we\u2019d expect.<\/p>\n<pre>.\/script-4.sh<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782547\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/8-7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a script with an error in a pipe chain.\" width=\"644\" height=\"190\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The first zero is the output from <code>wc<\/code>, telling us it didn\u2019t read any lines for the missing file. The second zero is the return code from the second <code>echo<\/code> command.<\/p>\n<p>We\u2019ll add in the <code>-o pipefail<\/code> , save it as \u201cscript-5.sh\u201d, and make it executable.<\/p>\n<pre>#!\/bin\/bash &#13;\nset -eo pipefail&#13;\n&#13;\necho This will happen first&#13;\ncat script-99.sh | wc -l&#13;\necho This will happen second<\/pre>\n<p>Let\u2019s run that and check the return code.<\/p>\n<pre>.\/script-5.sh<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782548\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/9-6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a script that traps errors in pipe chains and correctly sets the return code.\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The script halts and the second <code>echo<\/code> command isn\u2019t executed. The return code sent to the shell is one, correctly indicating a failure.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Use the Echo Command on Linux<\/em><\/strong><\/p>\n<h2 id=\"catching-uninitialized-variables\"><span class=\"ez-toc-section\" id=\"Catching_Uninitialized_Variables\"><\/span>Catching Uninitialized Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Uninitialized variables can be difficult to spot in a real-world script. If we try to <code>echo<\/code> the value of an uninitialized variable, <code>echo<\/code> simply prints a blank line. It doesn\u2019t raise an error message. The rest of the script will continue to execute.<\/p>\n<p>This is script-6.sh.<\/p>\n<pre>#!\/bin\/bash &#13;\nset -eo pipefail&#13;\n&#13;\necho \"$notset\" &#13;\necho \"Another echo command\"<\/pre>\n<p>We\u2019ll run it and observe its behavior.<\/p>\n<pre>.\/script-6.sh<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782551\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/10-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a script that doesn't capture uninitialized variables.\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The script steps over the uninitialized variable, and continues to execute. The return code is zero. Trying to find an error like this in a very long and complicated script can be very difficult.<\/p>\n<p>We can trap this type of error using the <code>set -u<\/code> (unset) option. We\u2019ll add that to our growing collection of set options at the top of the script, save it as \u201cscript-7.sh\u201d, and make it executable.<\/p>\n<pre>#!\/bin\/bash &#13;\n&#13;\nset -eou pipefail&#13;\n&#13;\necho \"$notset\" &#13;\n&#13;\necho \"Another echo command\"<\/pre>\n<p>Let\u2019s run the script:<\/p>\n<pre>.\/script-7.sh<\/pre>\n<pre>echo $?<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782552\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/11-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a script that does capture uninitialized variables.\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The uninitialized variable is detected, the script halts, and the return code is set to one.<\/p>\n<p>The <code>-u<\/code> (unset) option is intelligent enough <em>not to be triggered<\/em> by situations where you can legitimately interact with an uninitialized variable.<\/p>\n<p>In \u201cscript-8.sh\u201d, the script checks whether the variable <code>New_Var<\/code> is initialized or not.\u00a0You don\u2019t want the script to stop here, in a real-world script you\u2019ll perform further processing and deal with the situation yourself.<\/p>\n<p>Note that we\u2019ve added the <code>-u<\/code> option as the <em>second<\/em> option in the set statement. The <code>-o pipefail<\/code> option must come last.<\/p>\n<pre>#!\/bin\/bash &#13;\n&#13;\nset -euo pipefail&#13;\n&#13;\nif [ -z \"${New_Var:-}\" ]; then &#13;\n&#13;\necho \"New_Var has no value assigned to it.\" &#13;\n&#13;\nfi<\/pre>\n<p>In \u201cscript-9.sh\u201d, the uninitialized variable is tested and if it is uninitialized, a default value is provided instead.<\/p>\n<pre>#!\/bin\/bash&#13;\nset -euo pipefail&#13;\n&#13;\ndefault_value=484&#13;\nValue=${New_Var:-$default_value}&#13;\necho \"New_Var=$Value\"<\/pre>\n<p>The scripts are allowed to run through to their completion.<\/p>\n<pre>.\/script-8.sh<\/pre>\n<pre>.\/script-9.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782553\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/12-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running two scripts where the uninitialized variables are handled internally, and the -u option doesn't trigger.\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"sealed-with-a-x\"><span class=\"ez-toc-section\" id=\"Sealed_With_a_x\"><\/span>Sealed With a x<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another handy option to use is the <code>set -x<\/code> (execute and print) option. When you\u2019re writing scripts, this is can be a lifesaver. it prints the commands and their parameters as they are executed.<\/p>\n<p>It gives you a quick \u201crough and ready\u201d form of execution trace. Isolating logic flaws and spotting bugs becomes much, much easier.<\/p>\n<p>We\u2019ll add the set -x option to \u201cscript-8.sh\u201d, save it as \u201cscript-10.sh\u201d, and make it executable.<\/p>\n<pre>#!\/bin\/bash&#13;\nset -euxo pipefail&#13;\n&#13;\nif [ -z \"${New_Var:-}\" ]; then&#13;\n  echo \"New_Var has no value assigned to it.\"&#13;\nfi<\/pre>\n<p>Run it to see the trace lines.<\/p>\n<pre>.\/script-10.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-782554\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/01\/13-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running a script with -x trace lines written to the terminal.\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Spotting bugs in these trivial example scripts is easy. When you start to write more involved scripts, these options will prove their worth.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/782514\/how-to-use-set-and-pipefail-in-bash-scripts-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How To Use set and pipefail in Bash Scripts on Linux&#8221; fatmawati achmad zaenuri\/Shutterstock.com The Linux set and pipefail commands dictate what happens when a failure occurs in a Bash script. There\u2019s more to think about than should it stop or should it carry on. RELATED: The Beginner&#8217;s Guide to Shell Scripting: The Basics Bash&#8230;<\/p>\n","protected":false},"author":1,"featured_media":417929,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-417928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/417928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=417928"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/417928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/417929"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=417928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=417928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=417928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}