当前位置:网站首页>Shell脚本-位置参数(命令行参数)
Shell脚本-位置参数(命令行参数)
2022-07-01 08:36:00 【小蜗牛的路】
运行 Shell 脚本文件时我们可以给它传递一些参数,这些参数在脚本文件内部可以使用$n的形式来接收,例如,$1 表示第一个参数,$2 表示第二个参数,依次类推。
给脚本文件传递位置参数
脚本如下:
#!/bin/bash
echo "name: $1"
echo "age: $2"
执行命令:sh test.sh jack 18,其中jack是第一个位置参数,18是第二个位置参数,两者之间以空格分隔。输出如下:
name: jack
age: 18
给函数传递位置参数
脚本如下:
#!/bin/bash
#定义函数
function func(){
echo "name: $1"
echo "age: $2"
}
#调用函数
func jack 20
输出:
name: jack
age: 20
注意事项
如果参数个数太多,达到或者超过了 10 个,那么就得用${n}的形式来接收了,例如${10}、${23}。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
- The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
- 中小企业固定资产管理办法哪种好?
- I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
- 1.jetson与摄像头的对接
- [untitled]
- V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
- IT 技术电子书 收藏
- Memory size end
- Share 7 books I read in the first half of 2022
- Audio-AudioRecord create(一)
猜你喜欢

vscode自定义各个区域的颜色

C语言指针的进阶(上篇)

Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘

Huawei machine test questions column subscription Guide

C basic knowledge review (Part 4 of 4)
![[JS reverse] MD5 encryption parameter cracking](/img/06/0610045d287f646479d6eb5021a067.png)
[JS reverse] MD5 encryption parameter cracking

MD文档中插入数学公式,Typora中插入数学公式
![[untitled]](/img/40/560c597d53d27eff860644cd7c3030.png)
[untitled]
随机推荐
3、Modbus通讯协议详解
Gateway-88
SPL-安装与基本使用(二)
shardingSphere
1. Connection between Jetson and camera
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
View drawing process analysis
Leetcode t40: combined sum II
2022.2.15
Do you know how data is stored? (C integer and floating point)
中断与其他函数共享变量、临界资源的保护
2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
Glitch free clock switching technology
爬虫知识点总结
嵌入式工程师常见面试题2-MCU_STM32
2022 ordinary scaffolder (special type of construction work) examination question bank and the latest analysis of ordinary scaffolder (special type of construction work)
Yolov3, 4, 5 and 6 Summary of target detection
C语言指针的进阶(下)
NIO-零拷贝
[深度剖析C语言] —— 数据在内存中的存储