Shell中变量$#,$@,$0,$1,$2,$*,$$,$?的含义

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yaoshenjie/article/details/72843935


1.先写一个简单脚本,将这些值输出看下:

创建一个mytest.sh

输入以下内容:

#!/bin/sh

echo "#:$#"
echo "0:$0"
echo "1:$1"
echo "2:$2"
echo "@:$@"
echo "*:$*"
echo "$:$$"
echo "?:$?"


2.再利用chmod +x 改变文件可执行权限。

3.运行,查看输出对比

[root@localhost testutf8togdb]# ./mytest.sh 
#:0
0:./mytest.sh
1:
2:
@:
*:
$:13584
?:0
[root@localhost testutf8togdb]# ./mytest.sh aa
#:1
0:./mytest.sh
1:aa
2:
@:aa
*:aa
$:14127
?:0
[root@localhost testutf8togdb]# ./mytest.sh aa bb
#:2
0:./mytest.sh
1:aa
2:bb
@:aa bb
*:aa bb
$:14369
?:0
[root@localhost testutf8togdb]# ./mytest.sh ./
#:1
0:./mytest.sh
1:./
2:
@:./
*:./
$:14514
?:0
[root@localhost testutf8togdb]# ./mytest.sh ./*
#:3
0:./mytest.sh
1:./change_gbk2utf8.sh
2:./change_utf8togbk.sh
@:./change_gbk2utf8.sh ./change_utf8togbk.sh ./mytest.sh
*:./change_gbk2utf8.sh ./change_utf8togbk.sh ./mytest.sh
$:14561
?:0


4.给出个变量的定义:

$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误


猜你喜欢

转载自blog.csdn.net/yaoshenjie/article/details/72843935
今日推荐