# script.sh
function main() {
echo 'I got ' $# ' args'
}
main $*
main $@
main "$*"
main "$@"
# output
$ sh script.sh 'a b c' d e
I got 5 args
I got 5 args
I got 1 args
I got 3 args
if [[ expression ]]; then
# ...
elif [[ expression ]]; then
# ...
else
# ...
fi
條件選擇
case VAR in
var1) cmd1 ;;
var2) cmd2 ;;
# ...
*) cmd ;;
esac
循環(huán)控制
for
for var in (list); do
# ...
break
continue
done
for ((i=1, j=10; i <= 10; i++, j--)); do
# ...
done
while
while ((1)); do
# ...
break
continue
done
while true; do
# ...
done
until
until [[ -n "" ]]; do
# ...
break
continue
done
select DAY in Mon Tue Wed Thu Fri Sat Sun; do
case $DAY in
Mon) echo "Today is Monday" ;;
Tue) echo "Today is Tuesday" ;;
# ...
*) echo "Unkown day, stop" && break ;;
esac
done
# 函數(shù)
```shell
function foo() {
local var=0
return $var
}