1. Overview

When dealing with variables in shell scripting, it’s important to account for the fact that a variable may have one of several states:

Thus, to avoid unexpected errors, we can use the ${param:-param} expression along with similar expressions to assign a default value to a variable under specific conditions.

In this tutorial, we’ll explore how to assign a default value to a variable using ${param:-param} and similar expressions. To test our methods, we’ll use the echo command along with basic shell variable assignments.

2. Parameter Expansion of ${param:-param}

If a variable, x, is unset or is the null string, then ${x:-default_value} expands to the default_value string. Importantly, no assignment to the x variable takes place, and the variable retains its original value.

2.1. Non-null

To test the expression, let’s start with the case when x takes on a non-null value:

$ x=value
$ echo "${x:-default_value}"
value

Since x is set and isn’t the null string, the parameter expansion of ${x:-default_value} returns the value of x.

2.2. Null and Unset

Next, let’s consider the case when we set x to the null string:

$ x=''
$ echo "${x:-default_value}"
default_value

In this case, the expression expands to the default_value string shown on the right-hand side of the :- syntax.

Finally, let’s consider the case when x is unset:

$ unset x
$ echo "${x:-default_value}"
default_value

Here, the ${x:-default_value} expression expands to the default_value string since x is unset. The x variable remains unset and no assignment takes place.

2.3. Assign to Variable

Notably, however, we can assign the result of the entire expression to another variable:

$ y="${x:-default_value}"
$ echo "$y"
default_value

The y variable is assigned the result of the ${x:-default_value} expression which, in this case, expands to the default_value string since x is unset.

2.4. Variable Values

Moreover, the right-hand side of the ${param:-param} expression can represent the value of some other variable, say z:

$ z=0
$ echo "${x:-$z}"
0

Here, the expression expands to the value of the z variable since x is unset.

2.5. Substring Expansion Modifications

Notably, the ${param:-param} syntax differs from that of substring expansion:

$ x='string'
$ echo "${x: -4}"
ring
$ echo "${x:(-4)}"
ring

In this case, we extract a substring from the x variable starting at index 4, counting from the end. To distinguish the negative indexing used in variable subsetting from the ${param:-param} expression, we either introduce a space between the colon and the negative sign, or place the negative index within parentheses.

3. Parameter Expansion of ${param-param}

If a variable, x, is unset, then ${x-default_value} expands to the default_value string. Importantly, no assignment to the x variable takes place, and the variable retains its original value.

The difference between this scenario and the one from earlier is that when x is set to the null string, the expression doesn’t expand to the default_value string.

3.1. Non-null and Null

Let’s test the expression beginning with the case when x assumes a non-null value:

$ x=value
$ echo "${x-default_value}"
value

We see that the expression expands to the value of the x variable since the variable is set.

If x is the null string, the ${x-default_value} expression also expands to the value of x:

$ x=''
$ echo "${x-default_value}"

$

Now, let’s see what happens in the third case.

3.2. Unset

However, when x is unset, the expression expands to the default_value string provided in the right-hand side:

$ unset x
$ echo "${x-default_value}"
default_value

Notably, no assignment occurs, and the x variable remains unset. Of course, we can also substitute the constant string with a variable.

4. Parameter Expansion of ${param:=param}

The expansion of ${param:=param} is very similar to that of ${param:-param} with one difference: variable assignment occurs in this case, specifically when the variable is unset or is the null string.

4.1. Non-null

Let’s test the expression when x takes on a non-null value:

$ x=value
$ echo "${x:=default_value}"
value

The expression expands to the value of x as expected. No variable assignment takes place in this case, and x retains its original value.

4.2. Null and Unset

However, when x is the null string, the ${x:=default_value} expression assigns the default_value string to x, and the expression expands to the new value:

$ x=''
$ echo "${x:=default_value}"
default_value
$ echo "$x"
default_value

We see that both, the x variable and the expression, expand to the default_value string.

Likewise, if x is unset, we obtain the same result as with the null string case. Therefore, the default_value string assigns to x, and the expression expands to the new value of x:

$ unset x
$ echo "${x:=default_value}"
default_value
$ echo "$x"
default_value

We see that the expression triggers variable assignment in this case, and both x and the ${x:=default_value} expression expand to the default_value string.

5. Parameter Expansion of ${param=param}

The expansion of the ${param=param} expression is very similar to that of ${param-param} with one difference: variable assignment occurs in this case, specifically when the variable is unset.

5.1. Non-null and Null

If we set x to a non-null value, the ${x=default_value} expression expands to the value of x, and no new assignment takes place:

$ x=value
$ echo "${x=default_value}"
value

Similarly, if we set x to the null string, the ${x=default_value} expression expands to the value of x, which is the null string, and no assignment takes place:

$ x=''
$ echo "${x=default_value}"

$

Now, let’s see what happens when x isn’t set.

5.2. Unset

Finally, if we unset x, then ${x=default_value} assigns the default_value string to x, and the expression expands to the newly assigned value:

$ unset x
$ echo "${x=default_value}"
default_value
$ echo "$x"
default_value

In this case, the interpreter assigns x the default_value string.

6. Tabular Summary

We can summarize the parameter expansion results for the different expressions in a table:

Expression

x is set and isn’t null

x is set to the null string

x is unset

${x:-default_value}

$x

default_value

default_value

${x-default_value}

$x

$x (null string)

default_value

${x:=default_value}

$x

default_value

default_value

${x=default_value}

$x

$x (null string)

default_value

Importantly, an assignment to variable x only occurs in the cases of ${x:=default_value} and ${x=default_value}.

7. Conclusion

In this article, we explored how to set a default value to a variable under various conditions using the ${param:-param} expression and similar constructs. In particular, the conditions involved cases when a variable takes on a non-null value, the null string, or becomes unset.