shell $*和$@区别

1月 9, 2018 |

当没有双引号时,$*和$@扩展为每个位置参数
出现在双引号中,"$*"扩展为将所有位置参数通过IFS(通常为空格)拼接为单个参数,而"$@"任然扩展为每个位置参数

#/bin/sh
echo "------test for \$*--------"
for x in $*; do  echo "$x---"; done
echo "------test for \"\$*\"--------"
for x in "$*"; do  echo "$x---"; done
echo "------test for \$@--------"
for x in $@; do  echo "$x---"; done
echo "------test for \"\$@\"--------"
for x in "$@"; do  echo "$x---"; done

./test.sh hello javacoder.cn

------test for $*--------
hello---
javacoder.cn---
------test for "$*"--------
hello javacoder.cn---
------test for $@--------
hello---
javacoder.cn---
------test for "$@"--------
hello---
javacoder.cn---

Posted in: Linux

Comments are closed.