2009-09-25 39 views

回答

11

是的,它的作用:

var=${var:-10} 

即使其他变量:

unset var 
export def=99 
echo ${var:-${def}} # gives '99' 
export var=7 
echo ${var:-${def}} # gives '7' 
+0

完美!正是我所期待的。肯定需要使用* other *变量。 – 2009-09-25 04:53:06

+0

严格来说,这里不需要'export'。 – 2009-09-25 09:13:02

6

是的!

从手册页:

${parameter:-word} 
     Use Default Values. If parameter is unset or null, the expansion of word 
     is substituted. Otherwise, the value of parameter is substituted. 
${parameter:=word} 
     Assign Default Values. If parameter is unset or null, the expansion of word 
     is assigned to parameter. The value of parameter is then substituted. 
     Positional parameters and special parameters may not be assigned to in this way. 
${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 inter‐ 
     active, exits. Otherwise, the value of parameter is substituted. 
${parameter:+word} 
     Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of 
     word is substituted. 
2
​​3210
+0

把这个:http://bash.pastebin.com/f4e14cd53放入一个文件并执行它回应控制台窗口什么也没有。 – 2009-09-25 04:24:53

+0

我一定是做错了,那实际上工作 – 2009-09-25 04:35:48

+0

顺便说一句,你的答案也很棒!我只是喜欢额外的括号;) – 2009-09-25 04:57:51