2009-09-25 3 views

답변

11

예, 그것은 수행합니다

var=${var:-10} 

에도 다른 변수 :

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

완벽하게! 정확히 내가 무엇을 찾고 있었는지. * 다른 * 변수를 사용해야합니다. –

+0

엄밀히 말하면 '수출'은 여기에 필요하지 않습니다. –

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
$ default=10 
$ var=${var:-$default} 
$ echo $var 
10 
$ var=9 
$ var=${var:-$default} 
$ echo $var 
9 
+0

정확하게 이것을 넣기 : http://bash.pastebin.com/f4e14cd53 파일에 넣고 실행하면 콘솔 창에 아무것도 표시되지 않습니다. –

+0

내가 뭔가 잘못 했음에 틀림 없다. 실제로는 작동한다. –

+0

btw, 당신의 대답도 훌륭했다! 나는 여분의 괄호를 좋아한다;) –

관련 문제