当前位置:网站首页>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}。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
- 基础:3.opencv快速入门图像和视频
- Memory size end
- What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
- 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
- 为什么LTD独立站就是Web3.0网站!
- 你了解数据是如何存储的吗?(C整型和浮点型两类)
- It is designed with high bandwidth, which is almost processed into an open circuit?
- The data analyst will be ruined without project experience. These 8 project resources will not be taken away
- 动态代理
- Mavros sends a custom topic message to Px4
猜你喜欢
随机推荐
2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
一文纵览主流 NFT 市场平台版税、服务费设计
How to recruit Taobao anchor suitable for your own store
C basic knowledge review (Part 4 of 4)
[Yu Yue education] Shandong Vocational College talking about railway reference materials
Field agricultural irrigation system
Advanced API
C语言指针的进阶(上篇)
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
Nacos - 服务发现
中小企业固定资产管理办法哪种好?
个人装修笔记
Introduction to R language
爬虫知识点总结
SPL-安装与基本使用(二)
Bimianhongfu queren()
Screenshot tips
Foundation: 2 The essence of image
[MFC development (16)] tree control
Audio-AudioRecord create(一)









