Bash variables: Difference between revisions

From wikinotes
Line 12: Line 12:
<blockquote>
<blockquote>
<source lang="bash">
<source lang="bash">
test -z $var && echo "variable does not exist, or has no value"
test -z "$var" && echo "variable does not exist, or has no value"
test -n $var && echo "variable exists and has a nonzero value"
test -n "$var" && echo "variable exists and has a nonzero value"
</source>
</source>


There are many more options. See <code>man test</code>.
There are many more options. See <code>man test</code>.
</blockquote><!-- test variable -->
</blockquote><!-- test variable -->

Revision as of 02:04, 17 July 2021

Assignment

var="var"
FOO=${VARIABLE:=default} 

read -n 1 input  # user input to $input

test variable

test -z "$var" && echo "variable does not exist, or has no value"
test -n "$var" && echo "variable exists and has a nonzero value"

There are many more options. See man test.