当前位置:网站首页>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}。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
猜你喜欢
随机推荐
【面试必刷101】链表
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
C language student information management system
Introduction to R language
Redis源码学习(29),压缩列表学习,ziplist.c(二)
The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
MySQL8.0学习记录17 -Create Table
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
MATLAB【函数求导】
Nacos - Configuration Management
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
SPL Introduction (I)
How can enterprises and developers take the lead in the outbreak of cloud native landing?
《单片机原理与应用》——并行IO口原理
AES简单介绍
1. Connection between Jetson and camera
爬虫知识点总结
Matlab [functions and images]
Memory size end
Do you know how data is stored? (C integer and floating point)








