Jadu Kumar Saikia (jaduks) wrote,
Jadu Kumar Saikia
jaduks

Setting default value for a BASH variable

To set a default value for a BASH variable, the syntax is: (good for setting default value for a BASH command line parameter)

VARIABLE=${1:-DEFAULTVALUE}    #set VARIABLE with the value of 1st Arg to the script,
                                                          #If 1st arg is not entered, set it to DEFAULTVALUE

The following simple script illustrate this:

tmpdir=/tmp
defvalue=1

DIR=${1:-$tmpdir}   # Defaults to /tmp dir.
VALUE=${2:-$defvalue}           # Default value is 1.

echo $DIR
echo $VALUE


Now while running the script, specify values for both the arguments.
$ ./defvaue.sh /dev 23
/dev
23

This time don't mention their values.
$ ./defvaue.sh
/tmp
1


so

DIR=${1:-$tmpdir}
VALUE=${2:-$defvalue}


is a replacement for the following:

[ -z $1 ] && DIR="/tmp"
[ -z $2 ] && VALUE=1

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 0 comments