get a default value when variable is unset in make

后端 未结 4 1865
面向向阳花
面向向阳花 2020-12-31 09:10

(edit: question more accurate based on @Michael feedback)

In bash, I often use parameter expansion: the following commands print \"default value\" when

4条回答
  •  生来不讨喜
    2020-12-31 09:28

    Just remove the colon. If you use :- in your substitution the default value will be used if the variable is null, an empty string or it does not exist, but just using - on its own will only substitute the default value if the variable has not been defined.

    # var1=default
    # var2=
    
    # echo var2 is ${var2:-$var1}
    var2 is something
    # echo var3 is ${var3:-$var1}
    var3 is something
    
    # echo var2 is ${var2-$var1}
    var2 is
    # echo var3 is ${var3-$var1}
    var3 is something
    

提交回复
热议问题