shell命令行参数的判断 :? 和 :-

2023-11-26  本文已影响0人  吃豆腐不吐豆腐皮

${1:?}没有参数就报错return

#!bin/bash
#file name: test1.sh

bar=${1:?param empty}
echo $bar
test1.sh: line 3: 1: param empty
hi

${1:-}没有则给默认值

#!bin/bash
#file name: test2.sh

bar1=${1:-}
echo bar1:[$bar1]

bar2=${1:-default value}
echo bar2:[$bar2]
bar1:[]
bar2:[default value]
bar1:[hi]
bar2:[hi]
上一篇下一篇

猜你喜欢

热点阅读