Echo variable which references another variable in shell script

Statement: "$k"_Id_avg="$(grep -i "$k"_Id_avg file.txt)"and $k is a loop variable. Now I want to print the value stored in the above variable, how can I do it? I have tried echo "$k"_Id_avg but it prints only the variable name and replaces $k with the current loop value. eg: NM_Id_avg

1 Answer

I would suggest something like:

varname="$k"_Id_avg
echo ${!varname}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like