2014-09-25 48 views
0

在某些语言中,如果未设置,您可以将其他变量分配给另一个变量。像这样:如果未设置分配其他变量

// a should be === c 
var a = b | c // b is empty and c=1 

在bash你可以濑默认值,像这样:

a=${b:-c} 

但是这会给你c,而不是c

值我试过

a=${b:-$c} 

但这不起作用我该如何做?

+0

它应该; 'C = 5;一个= $ {B: - $ C}; echo $ a'输出5。 – chepner 2014-09-25 19:22:49

回答

2

a=${b:-$c}工作正常,只要c设置为某个值。否则a将被设置为null

c=4 
unset b 
a=${b:-$c} 
echo $a 

都给作为c

unset b 
unset c 
a=${b:-$c} 
echo $a 

4值输出会给输出(空)

1
${parameter:=word} 

从人的bash:

 
Assign Default Values. If parameter is unset or null, the expansion 
of word is assigned to parameter. The value of parameter is then 
substituted. 

${parameter:-word} 
 
Use Default Values. If parameter is unset or null, the expansion 
of word is substituted. Otherwise, the value of parameter is 
substituted.