linux命令下 $的含义,shell脚本中$#、$0等的含义

1、Shell脚本变量$#,$*等的含义

1、$#

表示执行脚本传入参数的个数

2、$*

表示执行脚本传入参数的列表(不包括$0)

3、$$

表示进程的id

4、$@

表示执行脚本传入参数的所有个数(不包括$0)

5、$0

表示执行的脚本名称

6、$1

表示第一个参数

7、$@

表示第二个参数

8、$?

表示脚本执行的状态,0表示正常,其他表示错误

例子:

#!/bin/bash

printf "the process id is %s\n" "$$"

printf "the return value is %s\n" "$?"

printf "the all argus is %s\n" "$*"

printf "the argus is %s\n" "$@"

printf "the number of argus is %s\n" "$#"

printf "the first argus0 is %s\n" "$0"

printf "the argus 1 is %s\n" "$1"

printf "the argus 2 is %s\n" "$2"

执行结果

tay@tay:/mnt/hgfs/hzs/shell$ ./shell.sh 123 456

the process id is 5386

the return value is 0

the all argus is 123 456

the argus is 123

the argus is 456

the number of argus is 2

the first argus0 is ./shell.sh

the argus 1 is 123

the argus 2 is 456

2、$*和$@的差异

在shell中,$@和$*都表示命令行所有的参数(不包含$0),但是$*将命令行所有的参数看成一个整体,而$@则区分各个参数

例子:

#!/bin/bash

echo "the all para:"

for i in "$@"

do

echo $i  #循环$#次

done

echo "the all para:"

for i in "$*"

do

echo $i

done

执行结果:

tay@tay:/mnt/hgfs/hzs/shell$ ./shell1.sh 1 2 3 4 5

the all para:

1

2

3

4

5

the all para:

1 2 3 4 5

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值