Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a question mark in bash variable parameter expansion as in ${var?}?

What is the meaning of a bash variable used like this:

 ${Server?} 
like image 797
Kai Avatar asked Jan 17 '12 03:01

Kai


People also ask

What does question mark mean in bash?

This control operator is used to check the status of last executed command. If status shows '0' then command was successfully executed and if shows '1' then command was a failure. The exit code of the previous command is stored in the shell variable $?.

What does $# mean in bash script?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.

What is bash parameter expansion?

Bash uses the value formed by expanding the rest of parameter as the new parameter ; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter . This is known as indirect expansion .

What is the meaning of ${ 1 in bash?

This answer is useful. This answer is not useful. Show activity on this post. ${1#*-} deletes the shortest match of *- , a glob-like pattern from $1 variable. E.g. abcdef-xyz-foo -> xyz-foo.


2 Answers

It works almost the same as (from the bash manpage):

${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

That particular variant checks to ensure the variable exists (is both defined and not null). If so, it uses it. If not, it outputs the error message specified by word (or a suitable one if there is no word) and terminates the script.

The actual difference between that and the non-colon version can be found in the bash manpage above the section quoted:

When not performing substring expansion, using the forms documented below, bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset.

In other words, the section above can be modified to read (basically taking out the "null" bits):

${parameter?word}
Display Error if Unset. If parameter is unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

The difference is illustrated thus:

pax> unset xyzzy ; export plugh=  pax> echo ${xyzzy:?no} bash: xyzzy: no  pax> echo ${plugh:?no} bash: plugh: no  pax> echo ${xyzzy?no} bash: xyzzy: no  pax> echo ${plugh?no}  pax> _ 

There, you can see that while both unset and null variable result in an error with :?, only the unset one errors with ?.

like image 125
paxdiablo Avatar answered Oct 12 '22 10:10

paxdiablo


It means that the script should abort if the variable isn't defined

Example:

#!/bin/bash echo We will see this ${Server?Oh no! server is undefined!} echo Should not get here 

This script will print out the first echo, and the "Oh no! ..." error message.

See all the variable substitutions for bash here: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

like image 23
Robert I. Jr. Avatar answered Oct 12 '22 10:10

Robert I. Jr.