BASH 脚本:当索引是变量时 $@ 的第 n 个参数? [英] BASH scripting: n-th parameter of $@ when the index is a variable?
本文介绍了BASH 脚本:当索引是变量时 $@ 的第 n 个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检索 $@ 的第 n 个参数(传递给脚本的命令行参数列表),其中 n 存储在一个变量中.
I want to retrieve the n-th parameter of $@ (the list of command line parameters passed to the script), where n is stored in a variable.
我试过 ${$n}.
例如,我想获取调用的第二个命令行参数:
For example, I want to get the 2nd command line parameter of an invocation:
./my_script.sh alpha beta gamma
并且索引不应该是显式的,而是存储在变量 n 中.
And the index should not be explicit but stored in a variable n.
源代码:
n=2
echo ${$n}
我希望输出为测试版",但出现错误:
I would expect the output to be "beta", but I get the error:
./my_script.sh: line 2: ${$n}: bad substitution
我做错了什么?
推荐答案
试试这个:
#!/bin/bash
args=("$@")
echo ${args[1]}
好的,用一些 $n 或其他东西替换1"...
okay replace the "1" with some $n or something...
这篇关于BASH 脚本:当索引是变量时 $@ 的第 n 个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文