简体   繁体   中英

what does “:?” mean in this shell command?

here is the shell script:

#!/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

And i come to confused about this line:

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

i look for the manual of shell,but get nothing about what does the ":?" actually mean.

thx

from 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.

in this case the parameter being checked is $1 (the first positional parameter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM