前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux中shell变量$#,$@,$0,$1,$2的含义解释

linux中shell变量$#,$@,$0,$1,$2的含义解释

作者头像
咻一咻
发布2020-05-29 11:49:59
1.6K0
发布2020-05-29 11:49:59
举报
文章被收录于专栏:咻一咻咻一咻

我们先来写一个简单的脚本,执行以后再看各个变量的意义。

[root@centos7 ~]#vim r1.sh 
#! /bin/bash
#  ------------------------------------------
#  Filename:    r1.sh
#  ...... //省略
# ------------------------------------------
printf "The complete list is %s\n" "$$"  //Shell本身的PID(ProcessID) 
printf "The complete list is %s\n" "$!"  //Shell最后运行的后台Process的PID 
printf "The complete list is %s\n" "$?"  //最后运行的命令的结束代码(返回值)
printf "The complete list is %s\n" "$*"  //所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
printf "The complete list is %s\n" "$@"  //所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
printf "The complete list is %s\n" "$#"  //添加到Shell的参数个数 
printf "The complete list is %s\n" "$0"  //Shell本身的文件名 
printf "The complete list is %s\n" "$1"  //添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
printf "The complete list is %s\n" "$2"  //添加到Shell的各参数值。$1是第1参数、$2是第2参数…。

执行结果:

[root@centos7 ~]#./r1.sh 123456 QQ
The complete list is 5327
The complete list is 
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456
The complete list is QQ
The complete list is 2
The complete list is ./r1.sh
The complete list is 123456
The complete list is QQ
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档