Concatenation Of Variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Concatenation Of Variables
# 1  
Old 08-11-2011
Concatenation Of Variables

Hi,

when I echo the values of two variables that have been set for example:

export FRUIT1=ORANGE
export FRUIT2=APPLE

echo $FRUIT1
ORANGE

echo $FRUIT2
APPLE

as expected but when I echo the following:
echo ${FRUIT1}AND{$FRUIT2}

I was expecting to get:
ORANGEANDAPPLE

but instead am getting:
APPLEEAND

So my FRUIT1 value is being partly overwritten instead. Why would this occur?

The environment I am using is SUSE Linux Enterprise Server 11 with the Korn shell (apologies if I need to give more specifics of my environment as I am not currently at work).

Regards,
Jaxim
# 2  
Old 08-11-2011
Code:
#  echo ${FRUIT1}AND{$FRUIT2}
echo ${FRUIT1}AND${FRUIT2}

$ in the wrong place
# 3  
Old 08-11-2011
Apologies for the typo on my part, Jim. I did try this earlier today at work with:
echo ${FRUIT1}AND${FRUIT2}
and I still got output of:
APPLEEAND
# 4  
Old 08-11-2011
I tested it on my system and it does work properly displaying:
Code:
ORANGEANDAPPLE

Can you show your current script with the "${" change?
# 5  
Old 08-11-2011
Hi,

Here is the script I have been using to test:

Code:
#!/bin/ksh

export FRUIT1=ORANGE
export FRUIT2=APPLE

echo $FRUIT1
echo $FRUIT2
echo ${FRUIT1}AND${FRUIT2}

# 6  
Old 08-15-2011
Works fine for me on Solaris. Try quoting the last line:
Code:
echo "${FRUIT1}AND${FRUIT2}"

or try using print instead of echo.
# 7  
Old 08-15-2011
Seems you have a carriage return in your output. From the look of it, it's emitted just before "APPLE". If so, after printing "ORANGE, the carriage return, \r, would cause the cursor to return to column 1. Then the first five letters of "APPLE" would overwrite the first five letters of "ORANGE", leaving the final "E" and the word "AND" afterwards.

You can test my hypothesis by filtering your echo through tr:
Code:
echo ${FRUIT1}AND${FRUIT2} | tr -d \\r

---------- Post updated at 09:55 AM ---------- Previous update was at 09:46 AM ----------


If that tr corrects your output, I'd check for a carriage return in the two spots in your code where I inserted the bold, red "\r".

Quote:
Originally Posted by jaxim
Hi,

Here is the script I have been using to test:

Code:
#!/bin/ksh

export FRUIT1=ORANGE
export FRUIT2=\rAPPLE

echo $FRUIT1
echo $FRUIT2
echo ${FRUIT1}AND\r${FRUIT2}

Regards,
Alister

Last edited by alister; 08-15-2011 at 12:01 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenation script

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

2. UNIX for Beginners Questions & Answers

Concatenation script

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

3. Shell Programming and Scripting

File concatenation

awk '{$2=$2":"$8"-"$3;$3=$NF;$4=$NF="";print $0 | $10=$10":"$8"-"$18;$11=$NF;$12=$NF="";print $0 }' design.txt Trying to concatenate specific fields in a spreadsheet and the others remain unchanged. I attached an excel spreadsheet (all the data comes from a design.txt), but I put an example... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Line concatenation help

please help me on this.... cat /xx.txt 2:1 2 2:2 24 8:0 0 9:0 0 Expected result would be 2:1 2 2:2 24 8:0 0 9:0 0 (4 Replies)
Discussion started by: Aditya.Gurgaon
4 Replies

5. Shell Programming and Scripting

Concatenation of two patterns

Read the lines from the file and concatenate the lines into single line then search the string "INPUT=" from the another file and if "INPUT" is present in that file assign the concatenate patterns into "INPUT=" pattern. example: file1.txt <some lines> INPUT = <some lines> file2.txt... (3 Replies)
Discussion started by: krsureshmca
3 Replies

6. Shell Programming and Scripting

String Concatenation

Hi All, I need to concatenate the values in the array into a variable. Currently the code is : for (( i=1 ; i <= $minCount ; i++ )) do var="${var}""${sample_file}" done The output is : /tmp/1/tmp/2/tmp/3/tmp/4/tmp/5/tmp/6/tmp/7/tmp/8/tmp/9/tmp/10 I need a space between... (1 Reply)
Discussion started by: sh_kk
1 Replies

7. Shell Programming and Scripting

Concatenation

How can I do this: date = 4 -----------> 04 Month= 3-----------> 03 I wish to make a varibale named Var1 which will hold the value of current date and month value as: 2 digits for date. 2 digits for month. So finally var1 should hold value as 0403 --- MMDD (11 Replies)
Discussion started by: Asteroid
11 Replies

8. UNIX for Dummies Questions & Answers

File Concatenation

Hi, I want to write a generic shell script, which can concatenate n number of files passed as parameter ;to an output file which is again a parameter Example catfl.sh outfl.txt a.txt b.txt c.txt (3 files to be concatenated into a file outfl.txt) catfl.sh outfl.txt a.txt b.txt(2 files to be... (3 Replies)
Discussion started by: samit_9999
3 Replies

9. UNIX for Dummies Questions & Answers

string concatenation

my input file contains thousands of lines like below 234A dept of education 9788 dept of commerce 8677 dept of engineering How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line 234A:dept of education 9788:dept of commerce 8677:dept of engineering (7 Replies)
Discussion started by: systemsb
7 Replies

10. Shell Programming and Scripting

Concatenation

What is syntax for String concatenation? I have $1 as directory. $var is some variable value '/' String value. How do I have to concatenate if I have to run utility - util $1 followed by '/' followed by $var There is no space between these three. (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question