BASH脚本:当索引是变量时,$ @的第n个参数? [英] BASH scripting: n-th parameter of $@ when the index is a variable?

查看:144
本文介绍了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}.

I tried ${$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}

我希望输出为"beta",但出现错误:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆