shell脚本中$*,$@,$#的区别


shell脚本中的$*,$@和$#

#!/bin/bash 
my_fun() { 
    echo "$#" 
} 
echo 'the number of parameter in "$@" is '$(my_fun "$@") 
echo 'the number of parameter in "$*" is '$(my_fun "$*")

 

 
 
执行:./my.sh p1 "p2 p3" p4后返回:
the number of parameter in "$@" is 3
the number of parameter in "$*" is 1

 



$*表示所有这些参数都被双引号引住。若一个脚本接收两个参数,$*等于$1$2
$@表示所有这些参数都分别被双引号引住,若一个脚本接收到两个参数,$@等价于$1$2
$#表示提供给脚本的参数号


举例说:
脚本名称叫test.sh 入参三个: 1 2 3
运行test.sh 1 2 3后
$*为"1 2 3"(一起被引号包住)
$@为"1" "2" "3"(分别被包住)
$#为3(参数数量)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM