echo the NAME of the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echo the NAME of the variable
# 1  
Old 04-25-2012
echo the NAME of the variable

Code:
#!/bin/bash
varA="AAA1"
varB="BBB2"
varC="CCC3"
for proccx in $varA $varB $varC do echo "the current name is ????? , the value is $proccx" echo " " done
#end of script

I want the output to look something like this:

the current name is varA, the value is AAA1

the current name is VarB, the value is BBB2

the current name is varC, the value is CCC3
# 2  
Old 04-25-2012
Use two arrays, or associative arrays (bash v4), or write them out again (you already did in the loop), or printf like so:

Code:
[mute@geek ~]$ varA=AAA1 varB=BBB2 varC=CCC3
[mute@geek ~]$ printf 'the current name is %s, the value is %s\n' varA "$varA" varB "$varB" varC "$varC"
the current name is varA, the value is AAA1
the current name is varB, the value is BBB2
the current name is varC, the value is CCC3

---------- Post updated at 12:46 PM ---------- Previous update was at 12:45 PM ----------

or bash's indirection:

Code:
$ for var in varA varB varC; do echo "$var is ${!var}"; done
varA is AAA1
varB is BBB2
varC is CCC3

# 3  
Old 04-25-2012
theres hundreds of vars

typing them out again is really not an option. I was hoping for a slick way to do this. I think I need to research arrays

All I seem to be able to do is echo the value of the variable, which makes sense because that is what I am passing to the for loop. maybe the answer lies in how the variable is inistially set. I dont know
# 4  
Old 04-25-2012
Did you see my post update about indirection? In this way you actually pass variables names rather than values, and use the special ${!} notation to expand the value of the variable inside the variable ...

edit: oh nevermind, saw your title "hundreds of vars"... indeed an array is the way to go. target is bash3 or bash4? with bash3 you'd need two separate arrays, one would keep the "key" and the other the "value",
in bash4 you have associative arrays, such that array[key]=val. example for bash3:

Code:
[mute@geek ~]$ keys=(varA varB varC varD)
[mute@geek ~]$ vals=(AAA1 BBB1 CCC1 DDD1)
[mute@geek ~]$ for i in "${!keys[@]}"; do echo "${keys[i]} = ${vals[i]}"; done
varA = AAA1
varB = BBB1
varC = CCC1
varD = DDD1


Last edited by neutronscott; 04-25-2012 at 03:55 PM..
# 5  
Old 04-25-2012
If you can get a delimiter char that isn't in the data or the variable name you could store var and value in 1 array under bash3:

Eg using : char
Code:
[chubler@unix.com ~]$ vars=(varA:AAA1 varB:BBB2 varC:CCC3 varD:DDD1)
[chubler@unix.com ~]$ for val in "${vars[@]}" ; do echo "${val%:*} = ${val#*:}"; done
varA = AAA1
varB = BBB2
varC = CCC3
varD = DDD1

# 6  
Old 04-25-2012
Try:

Code:
#!/bin/bash
varA=AAA1
varB=BBB2
varC=CCC3
for proccx in varA varB varC
do
  echo "the current name is $proccx, the value is ${!proccx}"
  echo " "
done


or POSIX:
Code:
varA=AAA1
varB=BBB2
varC=CCC3
for proccx in varA varB varC
do
  eval echo "\"the current name is $proccx , the value is \${$proccx}\""
  echo " "
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to echo a value of a var stored in another variable?

I'm writing a shell script in AIX and using ksh. I have a scenario where I have a variable A which stores $B. so when i echo "$A" it prints $B But I wish to print value stored in var b ie. \a\dir\res\ I wish to store \a\dir\res\ in a third variable C. later I want to cd into that path :... (1 Reply)
Discussion started by: simpltyansh
1 Replies

2. Shell Programming and Scripting

Echo awk output from its variable

Stumped with the formatting of the awk output when used with variables, e.g.: awk -F, 'BEGIN {OFS=","} print {$2,$3,$4}' $infile1 produces the desired output (with rows), but when echoing the variable below, the output is one continuous line var1=$(awk -F, 'BEGIN {OFS=","} print... (4 Replies)
Discussion started by: ux4me
4 Replies

3. Shell Programming and Scripting

Problem with variable ECHO $((SED...

Hi, I'm new here so I want to say hello to everyone first! I searched google and this forum for a similar problem, but wasn't successful #! /bin/bash I'm trying to output (echo) n lines of a text file to the screen (later into another file). But I have problem with the sed command, it won't... (1 Reply)
Discussion started by: studiologe
1 Replies

4. UNIX for Dummies Questions & Answers

How to assign echo in variable

I've testing the following code: echo test.txt | cut -d . -f1and get the output "text" So why can't i assign the command to a variable? VAR='"echo test.txt | cut -d . -f1"' echo $VAR (5 Replies)
Discussion started by: jl487
5 Replies

5. Shell Programming and Scripting

read variable after echo

Hi, i want to create an user-friendly script where you are asked for two numbers. i would like that these two number to be separated with "--" for example, but i can't figure out how to do this. for example read -p "Insert lowest and highest value: " min ; echo -n "-- "; read max so... (3 Replies)
Discussion started by: ezitoc
3 Replies

6. Shell Programming and Scripting

echo multiple variable with calculation

$total=500 echo "scale=2; $val1*100/$total" | bc echo "scale=2; $val2*100*100/$total" | bc echo "scale=2; $val3*100/$total" | bc I want to make the above code to be accomplish in a single echo line. For instance output:21.3, 44.2, 51.6 How to achieve that, some one please help, i just... (5 Replies)
Discussion started by: alvin0618
5 Replies

7. Shell Programming and Scripting

calling a variable to echo to a log

Hi everyone, I am trying to create a simple batch file to make SQL backups. this part of it works fine. Currently the script can mysql dump the databases, compress them, delete the .sql, compress the individual tar.gz into one larger one, delete the smaller files, encrypt the final tar.gz and... (1 Reply)
Discussion started by: luma
1 Replies

8. Shell Programming and Scripting

assigning variable value using echo

I am modifying an existing script and it has the following line: export SomeEnvVar=`echo ${SomeLocalVar}` Why wouldn't it just read: export SomeEnvVar=${SomeLocalVar} Is there some reason to use echo instead of a direct assignment? :confused: (2 Replies)
Discussion started by: shellburger
2 Replies

9. Shell Programming and Scripting

echo variable problem

hi I have say five variable. I would ask the user which one they want me to print and then print accordingly. TEST_1='10.2.3.4' TEST_2='11.2.3.5' TEST_3='12.2.3.5' TEST_4='13.2.3.5' TEST_5='14.2.3.5' print_var() { echo "Accessing var num $1" echo TEST$1 #??? But How do... (6 Replies)
Discussion started by: sabina
6 Replies

10. UNIX for Dummies Questions & Answers

Display from a variable using echo.

I have a variable that is outputting a lot of space. here has been 45 lines returned ... how can I remove the spaces between the "been and the 45" CODE: fil_len=`wc -l < coshb.txt` if ; then cat coshb.txt | more echo " " echo "There has been ${fil_len} lines... (4 Replies)
Discussion started by: jagannatha
4 Replies
Login or Register to Ask a Question