23 May 2021

I use quite often in my scripts parameter expansion.
So in this post I will explore what I use more but there are a lot more to explore and use :) , please see references for full documentation.

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

$ parameter="Test"
$ echo "${parameter:-word}"
Test
$ echo "$parameter"
Test

$ unset $parameter
$ echo "${parameter:-word}"
word
$ echo "$parameter"

${parameter:=word}

If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted.

$ parameter="Test"
$ echo ${parameter:=word}
Test
$ echo "$parameter"
Test

$ unset parameter
$ echo ${parameter:=word}
word
$ echo "$parameter"
word

${parameter:offset:length}

This is referred to as Substring Expansion. It expands to up to length characters of the value of parameter starting at the character specified by offset.

$ string=ipsedixit.org
$ echo ${string:4:5}
dixit

References