Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you're looking for is indirection.</p> <pre><code>assertNotEmpty() { : "${!1:? "$1 is empty, aborting."}" } </code></pre> <p>That causes the script to abort with an error message if you do something like this:</p> <pre><code>$ foo="" $ assertNotEmpty foo bash: !1: foo is empty, aborting. </code></pre> <p>If you just want to <strong>test</strong> whether <code>foo</code> is empty, instead of aborting the script, use this instead of a function:</p> <pre><code>[[ $foo ]] </code></pre> <p>For example:</p> <pre><code>until read -p "What is your name? " name &amp;&amp; [[ $name ]]; do echo "You didn't enter your name. Please, try again." &gt;&amp;2 done </code></pre> <p>Also, note that there is a <strong>very important difference</strong> between an <strong>empty</strong> and an <strong>unset</strong> parameter. You should take care not to confuse these terms! An empty parameter is one that is set but just set to an empty string. An unset parameter is one that doesn't exist at all.</p> <p>The previous examples all test for <strong>empty</strong> parameters. If you want to test for <strong>unset</strong> parameters and consider all set parameters OK, whether they're empty or not, use this:</p> <pre><code>[[ ! $foo &amp;&amp; ${foo-_} ]] </code></pre> <p>Use it in a function like this:</p> <pre><code>assertIsSet() { [[ ! ${!1} &amp;&amp; ${!1-_} ]] &amp;&amp; { echo "$1 is not set, aborting." &gt;&amp;2 exit 1 } } </code></pre> <p>Which only aborts the script when the parameter name you pass denotes a parameter that isn't set:</p> <pre><code>$ ( foo="blah"; assertIsSet foo; echo "Still running." ) Still running. $ ( foo=""; assertIsSet foo; echo "Still running." ) Still running. $ ( unset foo; assertIsSet foo; echo "Still running." ) foo is not set, aborting. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload