Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-30-2010
Registered User
 

Join Date: Jul 2009
Posts: 60
Thanks: 8
Thanked 0 Times in 0 Posts
How to preserve space while concatenating strings? (KSH)

I have these


Code:
str1=$(echo "This is string one with spaces \n This is also my sentence 1")

When I echo $str1, it displays the new line character properly.

Now I have another new variable say str2.

I want to concatenate in this way.. str1 + newline character + and then str2.

That's I want the output to be:


Code:
This is string one with spaces
This is also my sentence 1
Content of str2

How do I do it?

I tried


Code:
$str1 = "$str1 $(echo "\n") $str2"

and


Code:
$str1 = "$str1 \n $str2"

and


Code:
$str1 = "$str1 $(echo $IFS) $str2"

None of those works

---------- Post updated at 12:36 PM ---------- Previous update was at 12:30 PM ----------

If I just say echo $str1 "\n" $str2 at the cmd prompt, it works fine. It does not work when I try to store it in variable... it loses the new line character when stored in a variable.
Sponsored Links
    #2  
Old 07-30-2010
karthigayan's Avatar
Registered User
 

Join Date: Apr 2009
Location: India , Chennai
Posts: 137
Thanks: 0
Thanked 3 Times in 3 Posts
Try this ,


Code:
str3=$(echo "$str1 \n $str2")
echo -e $str3

The Following User Says Thank You to karthigayan For This Useful Post:
dahlia84 (07-30-2010)
Sponsored Links
    #3  
Old 07-30-2010
ygemici ygemici is online now Forum Advisor  
sed_shell@LNU
 

Join Date: Feb 2010
Location: istanbul
Posts: 1,478
Thanks: 1
Thanked 247 Times in 240 Posts

Code:
# echo -e $str1"\nContent of str2"
This is string one with spaces
This is also my sentence 1
Content of str2

The Following User Says Thank You to ygemici For This Useful Post:
dahlia84 (07-30-2010)
    #4  
Old 07-30-2010
Registered User
 

Join Date: Jul 2009
Posts: 60
Thanks: 8
Thanked 0 Times in 0 Posts
Thanks both of you! It worked I am using something like this now...


Code:
s3=$(echo "$s1" "\n" "$s2")
echo $s3

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
CSH: Concatenating Strings, how to add new line character and functions? ragabonds Shell Programming and Scripting 0 05-04-2009 07:15 PM
concatenating strings tulip Shell Programming and Scripting 2 02-05-2009 01:59 PM
mailx: concatenating strings for message body (KSH) rockysfr Shell Programming and Scripting 1 07-29-2007 08:12 AM
concatenating strings.. jt_csv Shell Programming and Scripting 2 02-07-2007 09:09 PM
Concatenating Strings radhika03 Shell Programming and Scripting 2 09-09-2005 02:48 AM



All times are GMT -4. The time now is 04:01 AM.