Concatenate two variables and form the third variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate two variables and form the third variable
# 1  
Old 09-27-2013
Lightbulb Concatenate two variables and form the third variable

Hi Guys,
I was scratching my head for this for half a day... finally not successful Smilie
Following is the problem

I have a variable
$ var1=123
$ var2-234
$ var3=345

and another Variable
$ i=1

Now i wanted to save these into a Variable as shown below
for i in 1 2 3
echo $con

Was kicked by saying
ksh: con=${var$i}: 0403-011 The specified substitution is not valid for this command.

my goal is the variable con should contain the values, when con is echoed (i.e 123,234 & 345)

Pls help ...Smilie

Last edited by Don Cragun; 09-27-2013 at 02:16 PM.. Reason: Added CODE tags.
# 2  
Old 09-27-2013
The shell thinks you're performing a parameter expansion.

Try this:


This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 09-27-2013
You can't directly expand a variable with a variable name. Here is a Korn shell script that provides four ways to do what you seem to be trying to do. Note that if the values you're assigning to var1, var2, or var3 are supplied by a user (or an untrusted external source of any kind), using eval can be dangerous:
for i in 1 2 3
do      eval con=\${var$i}
        echo "$con  (using eval)"

var=(ignored 123 234 345)
for i in 1 2 3
do      con=${var[$i]}
        echo "$con  (using ksh array w/subscript 1-3)"

var=(123 234 345)
for i in 0 1 2
do      con=${var[$i]}
        echo "$con  (using ksh array w/subscript 0-2)"

for con in 123 234 345
do      echo "$con  (using for loop directly)"

This script produces the output:
123  (using eval)
234  (using eval)
345  (using eval)

123  (using ksh array w/subscript 1-3)
234  (using ksh array w/subscript 1-3)
345  (using ksh array w/subscript 1-3)

123  (using ksh array w/subscript 0-2)
234  (using ksh array w/subscript 0-2)
345  (using ksh array w/subscript 0-2)

123  (using for loop directly)
234  (using for loop directly)
345  (using for loop directly)

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-27-2013
eval runs the evaluation twice
eval con=\$var$i

After the first evaluation this becomes e.g.

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 09-27-2013
Why are you trying to do this? Dynamic variable names are generally a terrible idea, and the usual things people want them for are often easily solved by other methods.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 09-30-2013
@in2nix4life: Thanks for your suggestion...Smilie, but when tried it out and echo con the result what i got is as below
$ echo $con

@Don Cragun, MadeInGermany: It worked perfectly...Smilie Thanks a lot...Smilie

@Corona688: This is just an idea which i got at that moment... I dont have any specific reason for this... Kindly let me know the other methods to achieve this...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple variables using awk and for loop for web form submission

Hi My goal is to fill an HTML form and submit. What I have managed to do: 1. curl command to fill up the form and submit 2. a file which has the input curl command: curl -v -b cookie.txt -d __CSRFToken__=dc23d5da47953b3b390ec68d972af10380908b14 -d do=create -d a=open -d... (10 Replies)
Discussion started by: zorrox
10 Replies

2. Shell Programming and Scripting

How to concatenate Path name to a variable??

I have the path name in a Variable Ex: $XML_PATH_FLAG = /ebs/appl/u00/universe01/inbound/universeorders Now I have to pick up ALL XML files in this directory . In other words I have to pick up ALL the files /ebs/appl/u00/universe01/inbound/universeorders/F1.xml... (2 Replies)
Discussion started by: Pete.kriya
2 Replies

3. Shell Programming and Scripting

Help cannot concatenate Ksh variables ?

Cannot combine these two strings into one line, either as a 3rd variable or echo or printing ? Frustrating. for i in `cat /scripts/pathList.dat` do OldRepo= grep Oldhostname ${i}/.svn/entries | tail -1 NewRepo= grep Oldhostname ${i}/.svn/entries | tail -1 | sed '/Oldhostname/... (41 Replies)
Discussion started by: pcpinkerton
41 Replies

4. Shell Programming and Scripting

concatenate variables

I need to know how to concatenate variables in Debian. I am making a interactive script where it ask the user for info to add a user I pull the first letter from the first middle and last name into individual variables now i want to put them all in one variable so i can put it into useradd command ... (4 Replies)
Discussion started by: HackerSeabass
4 Replies

5. Shell Programming and Scripting

Variable concatenate

Hello, It might be stupid question But I will ask it any way:) var1="1 2 3 4" var2="5 6 7 8" var3=$var1\ $var2 var4="$var1\n$var2" echo "$var1" echo "$var2" echo "$var3" echo "$var4" The result of executing this code is as follow 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4\n5 6... (12 Replies)
Discussion started by: fdc2suxs
12 Replies

6. Shell Programming and Scripting

concatenate variables problem

Hello, I have a tricky problem: I have a $file with a variable number of occurrences of "ORA-" (in this case two) .......... EXP-00008: ORACLE error 3113 encountered ORA-03113: end-of-file on communication channel EXP-00056: ORACLE error 1403 encountered ORA-01403: no data found... (9 Replies)
Discussion started by: Laurentiu
9 Replies

7. UNIX for Dummies Questions & Answers

Concatenate a string to a variable

Hello All, How to concatenate a string to a variable in a script I'm having a file which is consisting of data and i need to extract the first line of the file and append it to a string. /tmp/samp.list containg 60000 I like to concatenate it with a string (SS_) grep -w SS_$(head -1... (1 Reply)
Discussion started by: nkamalkishore
1 Replies

8. Shell Programming and Scripting

how to concatenate values of two variables with an underscore(_) in between

Hi, I'm new to shell programming. I have two variables a and b a=val1 b=val2 could anyone kindly post the shell script to concatenate the values of variable a and b with an underscore(_) in between? The final output should be val1_val2. (8 Replies)
Discussion started by: badrimohanty
8 Replies

9. Shell Programming and Scripting

How to concatenate a string and a variable

I need a way to build variable in this manner: variable_$i Inside a for loop i need to create it. where i goes from 1 to 30.. and then i need to print them on screen with echo $variable_$i which is the best way to do this? (6 Replies)
Discussion started by: sreedivia
6 Replies

10. UNIX for Dummies Questions & Answers

Asking on concatenate variable

Hi , like to ask if we use ksh script to take in parameter into $1 and how do i concatenate the $1 value with some words into a variable?? Below is what i have written and i think is wrong ,how do i write it? datafile="Report" || $1 || ".xls" (Should become Report2000.xls) echo... (3 Replies)
Discussion started by: blueberry80
3 Replies
Login or Register to Ask a Question