---

HowTo: Use Bash Parameter Substitution Like A Pro

[ Thanks to An Anonymous Reader for
this link. ]

“The $ character is used for parameter expansion, and
command substitution. You can use it for manipulating and/or
expanding variables on demands without using external commands such
as sed or awk.

#1: Getting Up Default Shell Variables Value

“The syntax is as follows:

${parameter:-defaultValue}
var=${parameter:-defaultValue}

“If parameter not set, use defaultValue. In this example, your
shell script takes arguments supplied on the command line. You’d
like to provide default value so that the most common value can be
used without needing to type them every time. If variable $1 is not
set or passed, use root as default value for u:

u=${1:-root}

“Consider the following example:”


Complete Story

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis