Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell: What is the purpose of ${var:-} when var is unset or null?

Tags:

linux

bash

shell

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?

like image 641
Tom Hale Avatar asked Aug 12 '16 02:08

Tom Hale


People also ask

What is ${ variable in shell script?

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.

How do you check if a variable is empty or null in shell script?

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"

What is ${ 0 * in shell script?

${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.

What's the difference between ${ var and {$ var in bash?

$var and ${var} are exactly equivalent. "$var" and "${var}" are exactly equivalent.

Why do we use ${VAR} instead of $Var?

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.

How to find if a bash variable is set to null?

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"

What are unset variables used for in Bash shell?

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.

What is the use of ${Var} in shell script?

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.


2 Answers

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
like image 99
Matei David Avatar answered Nov 15 '22 03:11

Matei David


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.

like image 41
Jonathan Leffler Avatar answered Nov 15 '22 03:11

Jonathan Leffler