Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script Variable Substitution

I'm trying to combine some text to a variable and output the value of the combined variable. For example:

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

Output would be:

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

Any ideas?

If I'm not using the correct terminology please correct me.

Thanks, Jared

like image 475
jared Avatar asked Jul 11 '26 05:07

jared


1 Answers

Try the following:

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

test_param FILE

The ! before varName indicates that it should look up the variable based on the contents of $varName, so the output is:

FILE
testFILE
/tmp/some_file.log
like image 79
Andrew Clark Avatar answered Jul 13 '26 22:07

Andrew Clark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!