在 bash 中使用单个命令为 shell 变量分配默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2013547/
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-09 18:50:35  来源:igfitidea点击:

Assigning default values to shell variables with a single command in bash

bashshell

提问by Edward Q. Bridges

I have a whole bunch of tests on variables in a bash (3.00) shell script where if the variable is not set, then it assigns a default, e.g.:

我对 bash (3.00) shell 脚本中的变量进行了大量测试,如果未设置变量,则它会分配一个默认值,例如:

if [ -z "${VARIABLE}" ]; then 
    FOO='default'
else 
    FOO=${VARIABLE}
fi

I seem to recall there's some syntax to doing this in one line, something resembling a ternary operator, e.g.:

我似乎记得有一些语法可以在一行中执行此操作,类似于三元运算符,例如:

FOO=${ ${VARIABLE} : 'default' }

(though I know that won't work...)

(虽然我知道那行不通……)

Am I crazy, or does something like that exist?

我疯了,还是存在类似的东西?

回答by Andrew McGregor

Very close to what you posted, actually:

非常接近您发布的内容,实际上:

FOO=${VARIABLE:-default}  # If variable not set or null, use default.

Or, which will assign defaultto VARIABLEas well:

或者,这也将分配defaultVARIABLE

FOO=${VARIABLE:=default}  # If variable not set or null, set it to default.

回答by miku

For command line arguments:

对于命令行参数:

VARIABLE=${1:-DEFAULTVALUE}    

which assigns to VARIABLEthe value of the 1st argument passed to the script or the value of DEFAULTVALUEif no such argument was passed.

它分配给VARIABLE传递给脚本的第一个参数的值,或者DEFAULTVALUE如果没有传递这样的参数,则分配给它的值。

回答by Hans Ginzel

If the variable is same, then

如果变量相同,则

: "${VARIABLE:=DEFAULT_VALUE}"

assigns DEFAULT_VALUE to VARIABLE if not defined. The double quotes prevent globbing and word splitting.

如果未定义,则将 DEFAULT_VALUE 分配给 VARIABLE。双引号可防止通配和分词。

Also see Section 3.5.3, Shell Parameter Expansion, in the Bash manual.

另请参阅Bash 手册中的第 3.5.3 节,Shell 参数扩展

回答by Montells

Even you can use like default value the value of another variable

甚至你可以像默认值一样使用另一个变量的值

having a file defvalue.sh

有一个文件 defvalue.sh

#!/bin/bash
variable1=
variable2=${2:-$variable1}

echo $variable1
echo $variable2

run ./defvalue.sh first-value second-valueoutput

运行./defvalue.sh first-value second-value输出

first-value
second-value

and run ./defvalue.sh first-valueoutput

并运行./defvalue.sh first-value输出

first-value
first-value

回答by ghostdog74

see hereunder 3.5.3(shell parameter expansion)

这里3.5.3(shell参数扩展)

so in your case

所以在你的情况下

${VARIABLE:-default}

回答by Guru

To answer your question and on all variable substitutions

回答您的问题和所有变量替换

echo "$\{var}"
echo "Substitute the value of var."


echo "$\{var:-word}"
echo "If var is null or unset, word is substituted for var. The value of var does not change."


echo "$\{var:=word}"
echo "If var is null or unset, var is set to the value of word."


echo "$\{var:?message}"
echo "If var is null or unset, message is printed to standard error. This checks that variables are set correctly."


echo "$\{var:+word}"
echo "If var is set, word is substituted for var. The value of var does not change."

回答by Rob Wells

Then there's the way of expressing your 'if' construct more tersely:

然后是更简洁地表达“if”结构的方法:

FOO='default'
[ -n "${VARIABLE}" ] && FOO=${VARIABLE}

回答by Curtis Mattoon

FWIW, you can provide an error message like so:

FWIW,您可以提供如下错误消息:

USERNAME=${1:?"Specify a username"}

This displays a message like this and exits with code 1:

这会显示一条类似这样的消息并以代码 1 退出:

./myscript.sh
./myscript.sh: line 2: 1: Specify a username

A more complete example of everything:

一个更完整的例子:

#!/bin/bash
ACTION=${1:?"Specify 'action' as argv[1]"}
DIRNAME=${2:-$PWD}
OUTPUT_DIR=${3:-${HOMEDIR:-"/tmp"}}

echo "$ACTION"
echo "$DIRNAME"
echo "$OUTPUT_DIR"

Output:

输出:

$ ./script.sh foo
foo
/path/to/pwd
/tmp

$ export HOMEDIR=/home/myuser
$ ./script.sh foo
foo
/path/to/pwd
/home/myuser
  • $ACTIONtakes the value of the first argument, and exits if empty
  • $DIRNAMEis the 2nd argument, and defaults to the current directory
  • $OUTPUT_DIRis the 3rd argument, or $HOMEDIR(if defined), else, /tmp. This works on OS X, but I'm not positive that it's portable.
  • $ACTION取第一个参数的值,如果为空则退出
  • $DIRNAME是第二个参数,默认为当前目录
  • $OUTPUT_DIR是第三个参数,或$HOMEDIR(如果已定义),否则,/tmp。这适用于 OS X,但我不确定它是否可移植。

回答by Assaf Shomer

Here is an example

这是一个例子

#!/bin/bash

default='default_value'
value=${1:-$default}

echo "value: [$value]"

save this as script.sh and make it executable. run it without params

将其保存为 script.sh 并使其可执行。不带参数运行它

./script.sh
> value: [default_value]

run it with param

用参数运行它

./script.sh my_value
> value: [my_value]