Bash variables: Difference between revisions

From wikinotes
Line 3: Line 3:
<source lang="bash">
<source lang="bash">
var="var"
var="var"
FOO=${VARIABLE:=default}  
FOO=${VARIABLE:=default} # FOO=$VARIABLE if exists, otherwise "default"


read -n 1 input  # user input to $input
read -n 1 input  # user input to $input

Revision as of 13:41, 17 July 2021

Assignment

var="var"
FOO=${VARIABLE:=default}  # FOO=$VARIABLE if exists, otherwise "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.