1
JOBS=${JOBS:="-j2"}

dir=${1-/usr/src}

:=这里是什么-意思?

我猜它们是某种默认值,那有什么区别呢?

4

2 回答 2

3

对于:=(和相关的=),您可以使用内置的 ':' 命令来评估参数而无需将其分配给自身:

# Set JOBS=-j2 if JOBS is not set or equal to ""
: ${JOBS:='-j2'}

# Set JOBS=-j2 if JOBS is not set. Don't change if JOBS is already set to ""
: ${JOBS='-j2'}

对于:-and -,不要改变变量的值;只需在其位置使用第二个值:

# Set dir to /usr/src if $1 is not set, but don't set $1 itself
dir=${1-/usr/src}

# Set dir to /usr/src if $1 is not set or set to "", but don't set or change $1
dir=${1:-/usr/src}
于 2012-05-02T18:26:39.417 回答
2

man bash(该Parameter Expansion部分)将回答这些问题和相关问题。

摘抄:

   ${parameter:-word}
          Use Default Values.  If parameter is unset or null, the expansion of word is substituted.  Otherwise, the value of parame‐
          ter 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 interactive, 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  substi‐
          tuted.
于 2012-05-02T15:04:10.393 回答