分配默认值shell变量与在bash一个命令 [英] Assigning default values to shell variables with a single command in bash

查看:153
本文介绍了分配默认值shell变量与在bash一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆的有关变量的测试,在bash(3.00)的shell脚本,如果变量没有设置,那么它指定一个默认的,例如:

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.:

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?

推荐答案

非常接近你贴什么,实际上是:

Very close to what you posted, actually:

FOO=${VARIABLE:-default}

或者,将分配给变量:

FOO=${VARIABLE:=default}

这篇关于分配默认值shell变量与在bash一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆