Skip to content
Advertisement

Declaring variables with indirect reference with a prefix

I have my parameters stored in a file and calling them with a prefix which i get it has input. now, i am getting the input and prefixing it and storing as a new variable and then pointing my new variable indirectly to my actual variable to use in my script.

Is there a way to directly mention the indirect variable pointer to my main variable like value1=$(!$pk_value1) something like this, so that i can skip the new variable declaration. i have close to 10 variables to be declared which makes my code lengthy.

My current code :

JavaScript

values.sh (I have close to 300 variables declared here)

JavaScript

Is there a way to directly mention the indirect variable pointer + my name like value1=$(!$pk_value1) something like this, so that i can skip the new variable declaration. i have close to 10 variables to be declared which makes my code lengthy.

Advertisement

Answer

If your Bash is recent enough, then use the -n indirect variable attribute like this:

JavaScript

Alternate method with populating an associative array from the file values.sh:

JavaScript

Working of the Associative array population:

xargs -l1 will translate the stdio input stream’s lines (here: <values.sh) into arguments to a command.

The command called by xargs is bash -c, witch executes an inline script detailed here:

JavaScript

Finally the Associative array declaration and assignment declare -A values="($(commands))" gets the entries generated by the xarg and inline shell script commands.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement