In my Linux Mint 17.2 /etc/bash.bashrc
I see the following:
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
This is the first reference to the token debian_chroot
.
Why does this code use ${debian_chroot:-}
instead of just $debian_chroot
?
Bash's Shell Parameter Expansion says:
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
Here, "word" is null, so why bother substituting null for null?
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.
To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
${0} is the first argument of the script, i.e. the script name or path. If you launch your script as path/to/script.sh , then ${0} will be exactly that string: path/to/script.sh . The %/* part modifies the value of ${0} . It means: take all characters until / followed by a file name.
$var and ${var} are exactly equivalent. "$var" and "${var}" are exactly equivalent.
For a plain variable expansion, the only reason to use ${VAR}is when parsing would otherwise grab too many characters into the variable name, as in ${VAR1}_$VAR2(which without braces would be equivalent to ${VAR1_}$VAR2). Most adorned expansions (${VAR:=default}, ${VAR#prefix}, …) require braces.
Return true if a bash variable is unset or set to the null (empty) string: if [ -z "$var" ]; then echo "NULL"; else echo "Not NULL"; fi Another option to find if bash variable set to NULL: [ -z "$var" ] && echo "NULL"
They can be used to set options of the bash shell itself. The next example will clarify this. By default, the shell will treat unset variables as a variable having no value. By setting the -u option, the shell will treat any reference to unset variables as an error.
The $ {var} syntax is primarily used for delimiting ambiguous tokens. For example, consider the following: The braces are required to access the elements of an array and for other special expansions. For example: Most of the rest of your questions have to do with quoting, and how the shell tokenizes input.
The syntax ${debian_chroot:-}
prevents the shell from exiting if it is running with set -u
(crash when using undefined variables) and debian_chroot
is unset at that point.
You don't want a normal interactive shell to have set -u
(it would crash too easily), but it can be very useful in scripts.
To see this:
bash -c 'set -u; [ -z $a ]; echo ok' # error
bash -c 'set -u; a=; [ -z $a ]; echo ok' # ok
bash -c 'set -u; [ -z ${a:-} ]; echo ok' # ok
bash -c 'set -u; a=; [ -z ${a:-} ]; echo ok' # ok
The use of the "${variable:-}"
notation protects the script from an error if the shell is somehow invoked with -u
or executes set -u
— that causes a complaint when you use an undefined variable.
-u
Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion. An error message will be written to the standard error, and a non-interactive shell will exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With