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"
 }
0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.