2

With the following bash function, how can I get the function allow $2 to be optional, defaulting its value when nothing is passed for that argument ?

serputil ()
 {
  local opstring="$1"
  local sersel="$2"
 }
Dilna
  • 405
  • 1
  • 6

1 Answers1

1

Like this, using bash parameter expansion:

iserputil () {
  local opstring="$1"
  local sersel="${2:-DEFAULT}"
  echo "$opstring"
  echo "$sersel"
 }

iserputil ok

Output

ok
DEFAULT

If you run

iserputil ok ko

You will get

ok
ko
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223