${1:?}
沒有參數就報錯return
#!bin/bash
#file name: test1.sh
bar=${1:?param empty}
echo $bar
- 執(zhí)行
bash test1.sh
冻璃,報錯
test1.sh: line 3: 1: param empty
- 執(zhí)行
bash test1.sh hi
,輸出
hi
${1:-}
沒有則給默認值
#!bin/bash
#file name: test2.sh
bar1=${1:-}
echo bar1:[$bar1]
bar2=${1:-default value}
echo bar2:[$bar2]
- 執(zhí)行
bash test2.sh
债查,輸出
bar1:[]
bar2:[default value]
- 執(zhí)行
bash test1.sh hi
,輸出
bar1:[hi]
bar2:[hi]