?

Log in

No account? Create an account

Previous Entry | Next Entry

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

Comments

Profile

jaduks
Jadu Kumar Saikia

Latest Month

January 2008
S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  
Powered by LiveJournal.com
Designed by chasethestars