linux - Bash 脚本变量替换

标签 linux bash variables substitution

我正在尝试将一些文本组合到一个变量中并输出组合变量的值。例如:

testFILE=/tmp/some_file.log
function test_param {
echo $1
echo test$1
echo $(test$1) #this is where I want to output the value of the combined variable
}

test_param FILE

输出将是:

FILE
testFILE
/tmp/some_file.log  <-- this is what I can't figure out.

有什么想法吗?

如果我使用的术语不正确,请纠正我。

谢谢, 贾里德

最佳答案

尝试以下操作:

#!/bin/bash
testFILE=/tmp/some_file.log
function test_param {
    echo $1
    echo test$1
    varName=test$1
    echo ${!varName}
}

test_param FILE

varName前的!表示要根据$varName的内容查找变量,所以输出为:

FILE
testFILE
/tmp/some_file.log

关于linux - Bash 脚本变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9136099/

相关文章:

linux - 有什么方法可以从任何地方退出 bash 终端但不退出脚本吗?

python - 使用 while 循环添加和减去当前位置

linux - "find"和 "ls"与 GNU 并行

linux - 列出按时间排序的文件 key 对

ios - 如何在 bash 别名 Cordova 中包含目标模拟器

objective-c - Objective-C 中的变量声明差异

javascript - 获取浏览器中所有现有内置变量的完整列表

linux - 在linux中插入一行包含sed命令的文件

linux - 谁能推荐交叉源代码引用工具?

php - 疯狂的 crond 行为。不断制作失效的 bash 进程