Variable concatenate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable concatenate
# 1  
Old 02-25-2011
Variable concatenate

Hello,

It might be stupid question But I will ask it any waySmilie

Code:
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 7 8
Why the escape is not working, I want variable 4 to look like this
1 2 3 4
5 6 7 8
I change the code like this
Code:
var1="1 2 3 4" 
var2="5 6 7 8"

var3=$var1\ $var2
var4="$var1
$var2"

echo "$var1"
echo "$var2"
echo "$var3"
echo "$var4"

it worked but it is ugly.
# 2  
Old 02-25-2011
Most likely your echo implementation needs an explicit option, in order to interpolate escape sequences:

Code:
$ echo \\n
\n
$ echo -e \\n

$

# 3  
Old 02-25-2011
Hello,


I do not think it is related to echo. Usually I am using echo without any problem. I think it is related on how I concatenate the variables
Code:
var4="$var1\n$var2"


Last edited by radoulov; 02-25-2011 at 10:53 AM.. Reason: Code tags, please!
# 4  
Old 02-25-2011
It's because you're using an escape sequence in the variable value.
Did you try to use the -e option?

Code:
$ v1=a v2=b v3="$v1\n$v2"
$ echo "$v3"
a\nb
$ echo -e "$v3"
a
b

echo's behavior and options vary between systems,
consider using printf for greater portability.

Last edited by radoulov; 02-25-2011 at 11:14 AM..
# 5  
Old 02-25-2011
Alternatively consider using the POSIX version of the echo utility. See echo
# 6  
Old 02-25-2011
Hello,

Thanks for the reply, and you are right. However this behavior cause a problem for me in another scenarios. it is not just only echo.
For example

Code:
var1="1 2 
1 2" 
var2="5 6 
7 8
7 8"
var3="$var1\n$var2"
var4=$(echo "$var3" | sort | uniq)
echo "$var4"

and now the result is
1 2
1 2\n5 6
7 8

and I want it to be

1 2
5 6
7 8

I can solve it as how I did in my first post but this is ugly, because I have the variables in different rows.
# 7  
Old 02-25-2011
And why are you using a separate variables in the first place?
I would handle such a situation using an array, not scalar variables.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - concatenate string - strange variable scoping

Hello, I am trying to concatenate a string in a bash script like this: runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" " env | grep "$ENV_SUFFIX" | while read line; do envCmd="-e \"${line}\" " runCmd=$runCmd$envCmd echo $runCmd # here concatenation works fine done echo... (3 Replies)
Discussion started by: czabak
3 Replies

2. Shell Programming and Scripting

Concatenate two variables and form the third variable

Hi Guys, I was scratching my head for this for half a day... finally not successful :confused: 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 do... (5 Replies)
Discussion started by: ramprabhum
5 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Concatenate files

Hi I am trying to learn linux step by step an i am wondering can i use cat command for concatenate files but i want to place context of file1 to a specific position in file2 place of file 2 and not at the end as it dose on default? Thank you. (3 Replies)
Discussion started by: iliya24
3 Replies

5. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

6. Web Development

Concatenate Strings

hi..:) this is my sample part of my program.. $csv_output .= $row.",". $row.",". $row.",". $row.",". $row.",". ... (2 Replies)
Discussion started by: Jeneca
2 Replies

7. Shell Programming and Scripting

Need to create concatenate the shell variable with file content

Hi Guys, I have a file. Each record needs to inserted into a table. The table also have other columns which needs to be inserted with Shell variables. The following is the file. Error code None. Error Code 1 The shell script is having these variables. Name=Magesh Dep=Coding ... (1 Reply)
Discussion started by: mac4rfree
1 Replies

8. 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

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