Linux Bash脚本变量替换
我试图将一些文本组合到一个变量,并输出组合变量的值。例如:Linux Bash脚本变量替换,linux,bash,variables,substitution,Linux,Bash,Variables,Substitution,我试图将一些文本组合到一个变量,并输出组合变量的值。例如: testFILE=/tmp/some_file.log 功能测试参数{ 回声1美元 回声测试$1 echo$(test$1)#这是我想要输出组合变量值的地方 } 测试参数文件 产出将是: 文件 测试文件 /tmp/some_file.log尝试以下操作: #!/bin/bash testFILE=/tmp/some_file.log function test_param { echo $1 echo test$1
testFILE=/tmp/some_file.log
功能测试参数{
回声1美元
回声测试$1
echo$(test$1)#这是我想要输出组合变量值的地方
}
测试参数文件
产出将是:
文件
测试文件
/tmp/some_file.log尝试以下操作:
#!/bin/bash
testFILE=/tmp/some_file.log
function test_param {
echo $1
echo test$1
varName=test$1
echo ${!varName}
}
test_param FILE
varName
之前的code>表示它应该根据$varName
的内容查找变量,因此输出为:
FILE
testFILE
/tmp/some_file.log
使用${!varname}
testFILE=/tmp/some_file.log
function test_param {
local tmpname="test$1"
echo "$1 - $tmpname"
echo "${!tmpname}"
}
test_param FILE
这方面的产出:
FILE - testFILE
/tmp/some_file.log
你是说:
#!/bin/bash
testFILE=/tmp/some_file.log
function test_param {
echo $1
echo test$1
eval "echo \$test$1"
}
test_param FILE
输出:
FILE
testFILE
/tmp/some_file.log
试试这个:
testFILE=/tmp/some_file.log
function test_param {
echo $1
echo test$1
foo="test$1"
echo ${!foo}
}
${!foo}
是一个间接参数扩展。它表示获取foo
的值,并将其用作要展开的参数的名称。我认为您需要一个简单的变量名;我尝试了${!test$1}
但没有成功。这对我很有效。我都输出了结果,并将其作为另一个变量保存
#!/bin/bash
function concat
{
echo "Parameter: "$1
dummyVariable="some_variable"
echo "$1$dummyVariable"
newVariable="$1$dummyVariable"
echo "$newVariable"
}
concat "timmeragh "
exit 0
结果是:
Parameter: timmeragh
timmeragh some_variable
timmeragh some_variable
@bitbucket输出到哪里?或者我得到的结果是什么?想要得到正确的答案,你需要提供错误的答案作为线索。你有输出吗?请看,就这样。我知道必须有一种不用变量的方法。