linux中shell变量$#,[email protected],$0,$1,$2,$!,$$,$*,$-,[email protected]等很多个,很容易记错,这里再次整理一下,相关含义解释如下,并附上一个实践截图。

多看几次,多用几次,应该就记熟悉了。

 
变量说明: 
$$ 
Shell本身的PID(ProcessID) 
$! 
Shell最后运行的后台Process的PID 
$? 
最后运行的命令的结束代码(返回值) 
$- 
使用Set命令设定的Flag一览 
$* 
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 
[email protected] 
所有参数列表。如"[email protected]"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。 
$# 
添加到Shell的参数个数 
$0 
Shell本身的文件名 
$1~$n 
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。 

 

根据上面的解释,我做了一下实验,如下:

Linux shell中一些参数与变量简介

#!/bin/bash


printf "The complete list \$\$ is %s\n" "$$"
printf "The complete list \$\! is %s\n" "$!"
printf "The complete list \$\- is %s\n" "$-"
printf "The complete list \$\? is %s\n" "$?"
printf "The complete list \$\* is %s\n" "$*"
printf "The complete list \$\@ is %s\n" "[email protected]"
printf "The complete list \$\# is %s\n" "$#"
printf "The complete list \$\0 is %s\n" "$0"
printf "The complete list \$\1 is %s\n" "$1"
printf "The complete list \$\2 is %s\n" "$2"
printf "The complete list \$0,\$1,\$2,\$3,\$4 is %s\n" "$0,$1,$2,$3,$4"

Linux shell中一些参数与变量简介

shell执行结果如下:

Linux shell中一些参数与变量简介

对着再看几次,应该就记住了。

 

也就是说:

$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1是传递给该shell脚本的第一个参数
$2是传递给该shell脚本的第二个参数
[email protected] 是传给脚本的所有参数的列表

 



本文转自 念槐聚 博客园博客,原文链接:http://www.cnblogs.com/haochuang/p/6739593.html,如需转载请自行联系原作者

相关文章:

  • 2021-10-09
  • 2021-09-13
  • 2021-12-19
  • 2021-11-19
  • 2021-09-27
  • 2021-11-06
  • 2021-12-29
猜你喜欢
  • 2021-05-06
  • 2022-12-23
  • 2021-04-14
  • 2021-07-29
  • 2022-12-23
  • 2021-12-25
相关资源
相似解决方案