Default shell variables

Default shell variables can be set using the := syntax.

 

If a variable is not set, nothing will be display.

 

Consider, echo $myvar

 

If $myvar has not been set, nothing will be displayed.

 

However, it can be set as follows:

 

echo ${myvar:=SuperSport}

 

Similarly, a dash/minus can be used instead of the =

 

echo ${myvar:-SuperSport}

 

Once set, it can not be set again unless unset first.

 

#! /bin/bash

echo ${arg:=First}
myvar=Second
echo ${myvar:=Third}
unset myvar
echo ${myvar:=Fourth}

 

Output:

First
Second
Fourth