0

I need to work and understand a ksh script. The variable declaration is done in a different way I am used to:

STA=${1:-blabla}

I don't really understand what does the "1:-" stands for? Is it possible to make a loop with this variable declaration such as:

STA=${1:-blabla blabla2 blabla3}
2

${var:-x} means "if var is unset or empty, replace it with x". As such it depends on what you mean by "mak[ing] a loop".

If you make a loop by splitting on $IFS, then yes, you can use this to create a variable for a loop. However, if that's what you want to do, I'd recommend using a ksh array instead:

if [ "$#" -eq 0 ]; then
  set -A sta blabla blabla2 blabla3 # ksh88/pdksh/mksh/ksh93
  sta=(blabla blabla2 blabla3)      # ksh93/mksh
else
  sta=("$@") # use the positional parameters if provided
fi

for x in "${sta[@]}"; do [...]
1

${1} is the first argument to the script. The substitution modifier :- is explained in the manual page for ksh:

${name:-word}

If name is set and not NULL, it is substituted; otherwise, word is substituted.

Thus, if an argument is provided to your script or function, $STA will be assigned the value of that argument. If there is no ${1}, then $STA will be assigned the default value "blabla" instead.

This is not a loop operator; if you want to loop over several values you'd need syntax other than ${1:-blabla blabla2 blabla3}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.