千家信息网

linux中shell变量$#,$@,$0,$1,$2的含义是什么

发表于:2025-02-14 作者:千家信息网编辑
千家信息网最后更新 2025年02月14日,本篇内容介绍了"linux中shell变量$#,$@,$0,$1,$2的含义是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望
千家信息网最后更新 2025年02月14日linux中shell变量$#,$@,$0,$1,$2的含义是什么

本篇内容介绍了"linux中shell变量$#,$@,$0,$1,$2的含义是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

test.sh 内容如下:

#!/bin/bashprintf "The string is %s\n" "$$"printf "The string is %s\n" "$!"printf "The string is %s\n" "$?"printf "The string is %s\n" "$*"printf "The string is %s\n" "$@"printf "The string is %s\n" "$#"printf "The string is %s\n" "$0"printf "The string is %s\n" "$1"printf "The string is %s\n" "$2"
[root@cz_fbsdb500_06 ]# sh test.sh Im so diao The string is 6169The string is The string is 0The string is Im so diaoThe string is ImThe string is soThe string is diaoThe string is 3The string is test.shThe string is ImThe string is so

"linux中shell变量$#,$@,$0,$1,$2的含义是什么"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0