0

I have a config file in unix as

Variable_name:sequences:Status
ABC:1234:/path/txt

and I have a script in unix as

$ABC="select * from table"

I want to pass the variable name from file and use the variable in script with the value in script.

so I need to run script like

for i in /path/file_config;
 $Variable=ABC
 echo $variable # need result "select* from table"

Please help me in this

2
  • $ABC= and $Variable= are invalid. That's not how you assign to variables. Commented May 31, 2020 at 7:22
  • The answers given are different from how I understand your question. I think you have one variable name in the config file, and you want that string for selecting the SQL statement in the script. Something like var=ABC; grep "^\$${var}=" /path/config | cut -d '"' -f2 . Is that correct? Commented Jun 1, 2020 at 21:42

2 Answers 2

1

In bash (But not other shells), ${!foo} is an indirect reference that expands to the value of the variable named in $foo.

So,

ABC="select * from table"
name=ABC
echo "${!name}"

will display select * from table

Sign up to request clarification or add additional context in comments.

3 Comments

Please put the sql in single quotes, I don't want * replaces by filenames. (off-topic: I understand the question differently)
@WalterA Why would it get replaced with double quotes?
Sorry, my echo ${ABC} was wrong. I should have done echo "${ABC}".
0

Adding a slight improvement to Shawn's response above:

for Variable_name in `awk -F: '{ print $1 }' /path/file_config`; do
Variable=$Variable_name
echo ${!Variable}
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.