bash ${var?} 中bash变量参数扩展中问号的含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8889302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:19:41  来源:igfitidea点击:

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

bashvariablessyntax

提问by Kai

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

像这样使用的 bash 变量的含义是什么:

 ${Server?}

回答by paxdiablo

It works almost the same as (from the bashmanpage):

它的工作原理几乎与(来自bash联机帮助页)相同:

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

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

该特定变体检查以确保变量存在(既已定义又不为空)。如果是这样,它会使用它。如果没有,则输出由 指定的错误消息word(如果没有,则输出合适的错误消息word)并终止脚本。

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

该版本与非冒号版本之间的实际区别可以在bash引用部分上方的联机帮助页中找到:

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

当不执行子字符串扩展时,使用下面记录的形式,bash测试未设置或为空的参数。省略冒号会导致仅对未设置的参数进行测试。

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.

${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 andnull variable result in an error with :?, only the unset one errors with ?.

在那里,您可以看到,虽然 unsetnull 变量都会导致 错误:?,但只有 unset 会导致 错误?

回答by Robert I. Jr.

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

在此处查看 bash 的所有变量替换:https: //www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html