Chinaunix首页 | 论坛 | 博客
  • 博客访问: 391233
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2018-03-04 16:15:04

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

 

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

 

 


 

 

阅读(13867) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~