Is it possible to re-assign the variable form the loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it possible to re-assign the variable form the loop?
# 1  
Old 12-09-2007
Is it possible to re-assign the variable form the loop?

Good day, everyone!
I've got a small problem with re-assigning the variable form the loop. What I'm trying to do is:
Code:
#!/bin/bash/
VAR1="AAA"
VAR2="BBB"
VAR3="CCC"

for WORD in VAR1 VAR2 VAR3;
do 
$WORD="DDD"
echo $WORD
done

Smilie
That's the output and error messages:
Code:
-bash: VAR1=DDD: command not found
VAR1
-bash: VAR2=DDD: command not found
VAR2
-bash: VAR3=DDD: command not found
VAR3

I'd really appreciate if anybody could explain how to perform such operations.
Smilie
Thank you in advance for any suggestions
# 2  
Old 12-09-2007
You will need to use "eval".
# 3  
Old 12-09-2007
Thanks a lot!!!!! I got it!!!
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

Assign Values to a Variable in While Loop and Update the File

Hello, Could anyone please help me with Assigning a value to variable and then updating the value in the original file IFS='|' while read -r Serial_ID JOB_NAME STATUS do if then echo "Perform Fuctions" ???Assign STATUS to COMPLETED and Update File??? done <File (7 Replies)
Discussion started by: infernalhell
7 Replies

4. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

5. Shell Programming and Scripting

Assign variable name through loop

Hi As bash does not support multidimensional arrays (?), I need some help with a problem. What I want to do is to assign variable names containing a counter in a loop . what I want to do is basically something like this: #!/bin/bash for i in {1..8}; do var$i = "some command" done... (6 Replies)
Discussion started by: Tobbev
6 Replies

6. Shell Programming and Scripting

for Loop and get value form File

Hi I have file A.txt 3 4 5 6 Now two loop one loop for no of line and second loop as per number for i to n for j to 3... for j to 4.. ...... End for loop; Thanks (2 Replies)
Discussion started by: asavaliya
2 Replies

7. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

8. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

9. Shell Programming and Scripting

Re-assign variable's value through which FOR LOOP loops

Hi, I've a requirement where I want to re-assign the value in the variable through which FOR LOOP loops. For e.g. Snippet of code --------------- for i in $var do echo $i >> $tempFile var=`echo $another_var | awk -F" " '{print $1}'` done I am re-assigning var so... (2 Replies)
Discussion started by: dips_ag
2 Replies

10. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies
Login or Register to Ask a Question