$加数字在Shell中的含义

简介: $加数字在Shell中的含义

原文地址

[1−LinuxBashShellScriptingTutorialWiki(cyberciti.biz)](https://bash.cyberciti.biz/guide/1 - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)](https://bash.cyberciti.biz/guide/1−LinuxBashShellScriptingTutorialWiki(cyberciti.biz)](https://bash.cyberciti.biz/guide/1)

案例介绍

**1∗∗ isthefirstcommand−lineargumentpassedtotheshellscript.Also,knowasPositionalparameters.Forexample,1** is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, 1∗∗isthefirstcommand−lineargumentpassedtotheshellscript.Also,knowasPositionalparameters.Forexample,0, 1,1, 1,3, $4 and so on. If you run ./script.sh filename1 dir1, then。

**1∗∗是传递给shell脚本的∗∗第一个命令行参数∗∗。另外,也被称为∗∗位置参数∗∗。例如,1**是传递给shell脚本的**第一个命令行参数**。另外,也被称为**位置参数**。例如,1∗∗是传递给shell脚本的∗∗第一个命令行参数∗∗。另外,也被称为∗∗位置参数∗∗。例如,0、1、3、4等等。

比如如果你运行./script.sh filename1 dir1,那么:

  • $0 is the name of the script itself (script.sh)
  • $1 is the first argument (filename1)
  • $2 is the second argument (dir1)
  • $9 is the ninth argument
  • 10isthetenthargumentandmustbeenclosedinbracketsafter{10} is the tenth argument and must be enclosed in brackets after 10isthetenthargumentandmustbeenclosedinbracketsafter9.
  • ${11} is the eleventh argument.
  • $0 代表了脚本名称本身,比如这里的script.sh 就是$0的值。
  • $1 代表了跟在脚本后面的第一个参数,$1 = filename1
  • $2 代表跟在脚本后面的第二个参数,$2 = dir1。
  • $9 对应的到 $9 代表之后的第九个参数
  • ${10} 是第10个参数,必须在$9之后用括号括起来。
  • ${11} 是第11个参数。

What does $1 mean in Bash? $1 在Bash脚本的含义

Create a shell script named demo-args.sh as follows:

最快的理解方式是实际在Linux上创建一个测试文件,这里我们命名为 demo-args.sh ,通过vim新建一个文件,脚本的内容如下:


xander@xander:~$ vim demo-arges.sh

文件当中添加内容如下:


#!/bin/bash
script="$0"
first="$1"
second="$2"
tenth="${10}"
echo "The script name : $script"
echo "The first argument :  $first"
echo "The second argument : $second"
echo "The tenth and eleventh argument : $tenth and ${11}"

image.png


xander@xander:~$ ll
total 44
drwxr-x--- 4 xander xander 4096 Feb  3 13:12 ./
drwxr-xr-x 3 root   root   4096 Jan  8 03:12 ../
....
-rw-rw-r-- 1 xander xander  225 Feb  3 13:12 demo-arges.sh
....

因为新建的文件不具备x(可执行)权限,使用命令chmod +x demo-arges.sh为新建的脚本文件新增可执行权限。


xander@xander:~$ chmod +x demo-arges.sh

之后运行脚本,我们传入字母作为参数:


xander@xander:~$ ./demo-arges.sh foo bar one two a b c d e f z f z h
The script name : ./demo-arges.sh
The first argument :  foo
The second argument : bar
The tenth and eleventh argument : f and z

从结果来看可以验证之前介绍的方法。

1inbashfunctions1 in bash functions 1inbashfunctions1 在函数含义

Create a new script called func-args.sh;

创建一个名为func-args.sh的新脚本。


xander@xander:~$ vim func-args.sh

添加内容如下:


#!/bin/bash
die(){
  local m="$1"  # the first arg 
  local e=$2    # the second arg
  echo "$m" 
  exit $e
}
# if not enough args displayed, display an error and die
[ $# -eq 0 ] && die "Usage: $0 filename" 1
# Rest of script goes here
echo "We can start working the script..."

$# -eq 0 这个判断逻辑是如果传参为0的情况。

image.png

同样需要添加可执行权限:


xander@xander:~$ chmod +x func-args.sh

我们不传入任何参数,直接执行脚本,则会进入到如果显示的args不够多,则显示错误并结束这一步。注意这里的$0并不是脚本的名称。


xander@xander:~$ ./func-args.sh 
Usage: ./func-args.sh filename

我们在脚本中传入参数,结果正确执行:


xander@xander:~$ ./func-args.sh /etc/hosts
We can start working the script...

其他例子


fingerprints() {
         local file="$1"
         while read l; do
                 [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l
         done < $file
}



相关文章
|
JSON API 定位技术
京东商品评论API技术指南
京东商品评论API提供标准化接口,支持按评分、排序、分页获取商品评论数据,返回JSON格式的评论内容、用户信息、评分星级及热门标签等,助力电商数据分析与运营决策。
|
9月前
|
人工智能 监控 安全
阿里云部署OpenClaw(Clawdbot)接入QVeris:重构量化交易逻辑,AI全自动炒股,告别人工盯盘!
在AI赋能金融分析的浪潮中,个人投资者面临的核心痛点日益凸显:人工盯盘耗时耗力、市场动态难以及时捕捉、专业分析工具门槛高成本高。而OpenClaw(原Clawdbot/Moltbot)凭借开源灵活的架构,成为打造专属金融AI助手的首选——通过接入A股实时数据,它能实现24小时市场监控、涨跌预警、潜力股推荐等核心功能,彻底解放人工盯盘的繁琐。
5688 2
|
10月前
|
人工智能 缓存 数据可视化
2026年OpenClaw(Clawdbot)阿里云一键部署步骤流程及常见问题解答(FAQ)
OpenClaw(原Clawdbot)作为阿里云生态下轻量化、高适配的AI自动化代理工具,2026年推出的一键部署方案大幅降低了使用门槛,但用户在实际部署和使用过程中,仍会因环境配置、权限设置、跨境网络等问题遇到各类卡点。本文将先完整拆解阿里云OpenClaw一键部署的核心流程,再针对部署、功能使用、对接第三方平台、性能优化四大类高频问题给出详细解答,包含实操代码命令与避坑技巧,帮助用户快速解决使用中的各类问题。
1614 0
|
IDE 开发工具 Python
通义灵码+支付 MCP:30 分钟实现创作打赏智能体
本文介绍如何使用通义灵码智能体与 qwen3 和支付 MCP 编写创作打赏智能体,该智能体能够完成日常聊天、诗词创作和请求打赏并生成支付链接功能。
1139 1
7-5 sdut-C语言实验-最长公共子序列
7-5 sdut-C语言实验-最长公共子序列
523 0
|
资源调度 前端开发 JavaScript
Jest 测试实战指南
本文系统讲解如何使用 Jest 进行高效的 JavaScript 函数测试,涵盖环境搭建、测试用例编写、模拟函数与快照测试等内容,帮助开发者提升代码质量与测试效率。
539 0
|
安全 网络安全 Apache
如何为服务器生成一个TLS证书
通过以上步骤,您可以成功地为服务器生成并配置TLS证书,确保网站的安全性。本文涵盖了从生成私钥、创建CSR、使用自签名证书到获取正式证书的详细步骤,并且对每个步骤进行了详细的解释,使即使是非专业人士也能顺利完成TLS证书的配置。
2128 79
|
机器学习/深度学习 编解码 算法
论文阅读笔记 | 目标检测算法——DCN(可变形卷积网络)
论文阅读笔记 | 目标检测算法——DCN(可变形卷积网络)
1865 0
论文阅读笔记 | 目标检测算法——DCN(可变形卷积网络)
|
缓存 监控 容灾
饿了么技术往事(下)
在商业竞争中,技术团队和其他团队一样,作为组织的一部分,应该是荣辱与共的。但是,就行业而言,饿了么创造的社会价值也是显而易见的,而且,饿了么塑造了这个行业最初的模样,对社会创造的价值影响是长远的!
10496 128
饿了么技术往事(下)