什么是“:?”这个shell命令意味着什么?

时间:2012-03-06 07:31:27

标签: linux bash shell

这是shell脚本:

#!/bin/bash

version="0.01";

fibonacci() {
    n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.}
    if [ $n -le 1 ]; then 
    echo $n
    else
    l=`fibonacci $((n-1))`
    r=`fibonacci $((n-2))`
    echo $((l + r))
    fi
}

for i in `seq 1 10`
do
  result=$(fibonacci $i)
  echo "i=$i result=$result"
done

我对这一行感到困惑:

n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.}

我寻找shell的手册,但什么都没有得到“:?”实际上是指。

THX

1 个答案:

答案 0 :(得分:5)

来自man bash:

${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.

在这种情况下,被检查的参数是$ 1(第一个位置参数)