当前位置:网站首页>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}。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
- Redis publish subscription
- Foundation: 2 The essence of image
- The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
- Internet of things technology is widely used to promote intelligent water automation management
- 【MFC开发(16)】树形控件Tree Control
- Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
- 分享2022上半年我读过的7本书
- 2022 examination summary of quality controller civil engineering direction post skills (quality controller) and reexamination examination of quality controller civil engineering direction post skills
- Field agricultural irrigation system
- [JS reverse] MD5 encryption parameter cracking
猜你喜欢
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

R语言入门

What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR

Qt的模型与视图

C basic knowledge review (Part 4 of 4)

It is designed with high bandwidth, which is almost processed into an open circuit?

个人装修笔记

Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting

Intelligent water and fertilizer integrated control system

【MFC开发(17)】高级列表控件List Control
随机推荐
基于Gazebo的无人机管道检测
电脑小技巧
機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
避免按钮重复点击的小工具bimianchongfu.queren()
Vscode customize the color of each area
任务、线程、进程 区别
DID的使用指南,原理
Computer tips
Dynamic proxy
基础:2.图像的本质
挖财打新股安全吗
factory type_id::create过程解析
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
[MFC development (17)] advanced list control list control
【MFC开发(17)】高级列表控件List Control
Huawei machine test questions column subscription Guide
动态代理
毕业论文中word的使用1-代码域标公式
yolov5训练可视化指标的含义
截图小妙招