按名称间接查找 shell 变量
假设我有一个变量的名称存储在另一个变量中:
myvar=123
varname=myvar
现在,我想通过使用 $varname
变量来获取 123
。 有直接的方法吗?我发现没有这样的 bash 内置函数用于按名称查找,所以想出了这个:
function var { v="\$$1"; eval "echo "$v; }
所以
var $varname # gives 123
最后看起来还不错,但我想知道我是否错过了一些更明显的东西。
Let's say I have a variable's name stored in another variable:
myvar=123
varname=myvar
Now, I'd like to get 123
by just using $varname
variable.
Is there a direct way for that? I found no such bash builtin for lookup by name, so came up with this:
function var { v="\$1"; eval "echo "$v; }
so
var $varname # gives 123
Which doesn't look too bad in the end, but I'm wondering if I missed something more obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 bash 手册页:
From the man page of bash:
没有直接符合 Posix 的语法,只有 bash 主义。我通常这样做:
这适用于任何 Posix shell,包括那些以
bash
为登录 shell 且/bin/sh
更小、更快的系统,例如灰烬
。我喜欢 bash 并将其用于我的登录 shell,但我避免在命令文件中使用 bashism。Note: One problem with writing bash-specific scripts is that even if you can count on bash being installed, it could be anywhere on the path. It might be a good idea in that case to use the fully general
/usr/bin/env
shebang style, but note that this is still not 100% portable and has security issues.There isn't a direct Posix-conforming syntax, only a
bash
ism. I usually do this:This will work on any Posix shell, including those systems where
bash
is the login shell and/bin/sh
is something smaller and faster likeash
. I like bash and use it for my login shell but I avoid bashisms in command files.Note: One problem with writing bash-specific scripts is that even if you can count on bash being installed, it could be anywhere on the path. It might be a good idea in that case to use the fully general
/usr/bin/env
shebang style, but note that this is still not 100% portable and has security issues.${!varname}
应该可以解决问题${!varname}
should do the trick当我需要更新我的 Bash 技能时,我通常会查看高级 Bash 脚本指南。
关于您的问题,请查看 间接引用
符号为:
I usually look at Advance Bash-Scripting Guide when I need to freshen up my Bash skills.
Regarding your question look at Indirect References
Notation is:
我目前正在使用这个小功能。我对此并不完全满意,并且我在网上看到了不同的解决方案(如果我记得我会在这里写下它们),但它似乎有效。在这几行中已经有一些冗余和额外的数据,但它对调试很有帮助。
如果您想在适当的位置查看它,即我在哪里使用它,请检查:
https://github.com/mariotti/bmu/blob/ master/bin/backmeup.shellfunctions.sh
当然这不是最好的解决方案,但让我继续工作,在
希望我很快就能用更通用的东西取代它。
I am currently using this little function. I am not fully happy with it, and I have seen different solutions on the web (if I could recall I would write them here), but it seems to work. Within these few lines there is already some redundancy and extra data but it was helpful for debugging.
If you want to see it in place, i.e. where I am using it, check:
https://github.com/mariotti/bmu/blob/master/bin/backmeup.shellfunctions.sh
Of course it is not the best solution, but made me going on with the work, in
the hope I can replace it with something a bit more general soon.