如何在bash中获得第n个位置参数? [英] How to get the nth positional argument in bash?
本文介绍了如何在bash中获得第n个位置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Bash 中获取第 n
个位置参数,其中 n
是可变的?
How to get the n
th positional argument in Bash, where n
is variable?
推荐答案
使用 Bash 的间接功能:
Use Bash's indirection feature:
#!/bin/bash
n=3
echo ${!n}
运行该文件:
$ ./ind apple banana cantaloupe dates
产生:
cantaloupe
你也可以做数组切片:
echo ${@:$n:1}
但不是数组下标:
echo ${@[n]} # WON'T WORK
这篇关于如何在bash中获得第n个位置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文