0

bash --version

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Run the following and I'm wondering if there are any other cool ways to print / get the values (than using eval / requiring to create another variable).

a=giga; 
b=$a; 
echo a=$a --equals-- b=$b; 
echo; 

c=\$a; 
eval d_${a}_version=1.1.1;      ## I want to use this format. Use case: where possible values for 'a' are more than 100 and I don't want to create 100+ individual variables in my script.

echo c=$c; 
eval echo "c\'s \(dereferenced_value\)=$c";     ## Had to use eval to de-reference / get to a's value.
echo "d_giga_version = $(eval echo \$d_${a}_version)"; 

d_real_ver=d_${a}_version;                      ## Had to create another variable (wondering if there's another way to dereference d_giga_version variable's value.
echo "d_real_ver     = ${!d_real_ver}";

echo -e "\n\n-- HOW can i get value of 'c' == "giga" without using eval --OR--\n-- How can i get the value '1.1.1' (without requiring to create another new variable like \${d_real_ver}?\n\n"

It'll output:

a=giga --equals-- b=giga

c=$a
c's (dereferenced_value)=giga
d_giga_version = 1.1.1
d_real_ver     = 1.1.1


-- HOW can i get value of 'c' == giga without using eval --OR--
-- How can i get the value '1.1.1' (without requiring to create another new variable like ${d_real_ver}?

PS: My post/question that I'm trying to achieve is different than Bash - variable variables this post uses another variable to derefernece (see last 2 lines in output) for my questions.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
AKS
  • 16,482
  • 43
  • 166
  • 258
  • Do you have Bash 4 or newer? If so, you should use associative arrays. – Benjamin W. Jan 09 '20 at 21:44
  • @BenjaminW. thx I just updated it. – AKS Jan 09 '20 at 21:46
  • The second duplicate shows how to use associative arrays. – Benjamin W. Jan 09 '20 at 21:58
  • how can I pass `echo $c` (which gives '$a' as output) to another echo command using `|` pipe. I mean, if I `touch \$a` (which creates a `$a` named file and do `echo $c | xargs ls -l` it list `$a` file successfully as `-rw-r--r-- 1 112233 group_automation 0 Jan 9 22:06 $a` It seems like, my question could be how to do this ^^^^ by using `echo` on the output of $c which is '$a' like xargs worked for `ls -l`!! – AKS Jan 09 '20 at 22:08
  • @BenjaminW. I like that 2nd post it has a lot of info to learn. Thanks – AKS Jan 09 '20 at 22:33
  • 1
    ok did that DAMN thing... here: `echo $c | xargs -I {} sh -c 'echo {}' ` it will spit `giga` (without using `eval` and without using any other variable (at least visually)`).. now I can eat or sleep good! and now I can't even post this solution as the valid answer. – AKS Jan 09 '20 at 22:58
  • 1
    @ArunSangal, That `echo $c | xargs -I {} sh -c 'echo {}'` won't return any output unless the the variables are exported first. Example: `a=foo b=$a c=\$a ; echo $c | xargs -I {} sh -c 'echo {}'` returns nothing. But `export a=foo b=$a c=\$a ; echo $c | xargs -I {} sh -c 'echo {}'` returns *"foo"*. – agc Jan 10 '20 at 07:03
  • @agc thanks, I recall I did that during my hit-trial. – AKS Jan 10 '20 at 15:51

0 Answers0