使用字符串作为bash脚本变量名 [英] Use string as variable name in BASH script
本文介绍了使用字符串作为bash脚本变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下内容:
#!/bin/sh
n=('fred' 'bob')
f='n'
echo ${${f}[@]}
和我需要换人后底线执行
and I need that bottom line after substitutions to execute
echo ${n[@]}
没有办法做到这一点?我只是得到
any way to do this? I just get
test.sh: line 8: ${${f}}: bad substitution
在我结束。
推荐答案
您可以做可变间接,像这样的数组:
You can do variable indirection with arrays like this:
subst="$f[@]"
echo "${!subst}"
由于soulmerge笔记,你不应该使用#!/ bin / sh的
这一点。我使用#!的/ usr / bin中/ env的庆典
我的家当,无论在哪里Bash是在你的路径应该工作。
As soulmerge notes, you shouldn't use #!/bin/sh
for this. I use #!/usr/bin/env bash
as my shebang, which should work regardless of where Bash is in your path.
这篇关于使用字符串作为bash脚本变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文