How to preserve space while concatenating strings? (KSH)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to preserve space while concatenating strings? (KSH)
# 1  
Old 07-30-2010
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 Smilie

---------- 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.
# 2  
Old 07-30-2010
Try this ,

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

This User Gave Thanks to karthigayan For This Post:
# 3  
Old 07-30-2010
Code:
# echo -e $str1"\nContent of str2"
This is string one with spaces
This is also my sentence 1
Content of str2

This User Gave Thanks to ygemici For This Post:
# 4  
Old 07-30-2010
Thanks both of you! It worked Smilie I am using something like this now...

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preserve leading white space

I have about 350 programs in which I have to add 2 lines; one before and one after a specfic line. The following script does the job except that I lose the indentation. #!/usr/bin/bash while read line ... (8 Replies)
Discussion started by: jgt
8 Replies

2. Shell Programming and Scripting

Lex: concatenating word into array of c-strings

Alright, so I'm writing a file for the lexical analyzer (lex). It will be used to check C code (collecting the identifiers and storing those names along with the line numbers the identifier was found on). I'm not used to 'C' so I'm having some difficulty. I am using a function (insertId()) to... (4 Replies)
Discussion started by: D2K
4 Replies

3. Shell Programming and Scripting

Concatenating strings and run it in bash

Hi, all, I tried to write a simple shell script as follow: #!/bin/bash # What want to do in bash is following # : pcd_viewer cloud_cluster_0.pcd cloud_cluster_1.pcd cloud_cluster_2.pcd cloud_cluster_3.pcd cloud_cluster_4.pcd STR = "pcd_viewer" for i in `seq 0 4` do STR... (1 Reply)
Discussion started by: bedeK
1 Replies

4. Shell Programming and Scripting

Problem in concatenating two Strings

Hi Friends, I'm new to shell scripting and trying to concatenate two Strings to create a filepath like string but I'm getting an unexpected result. here is my code for 'runToneUserLoad.sh': script_dir="$(dirname $0)" echo "Script Dir:$script_dir" dirtest1="/installedUtility"... (6 Replies)
Discussion started by: kuldeept
6 Replies

5. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

6. Shell Programming and Scripting

How to preserve NL in Ksh variables?

I'm trying to set a variable to the output of a command. This is what the comand output to the display looks like: />hciconndump -v TOsiu Dump of connection(s): TOsiu ---------------------------------------------------------------------- Process: A60Tsiu Connection: TOsiu... (2 Replies)
Discussion started by: troym72
2 Replies

7. Shell Programming and Scripting

concatenating strings

I m new to shell scripting and what i want is take as an i/p from command line the name of the file and inside my script i should redirect the o/p of my few commands to this file concatenated with .txt for example if i give ./linux filename i should get the o/p in filename.txt i need to... (2 Replies)
Discussion started by: tulip
2 Replies

8. Shell Programming and Scripting

mailx: concatenating strings for message body (KSH)

Hi all, Think this is a pretty simple problem, but I've been thinking about it for a few days. Let's say that I'm going to have to output the contents of a file as the body of a mailx message. I'll probably do this: cat <filename> | mailx <extra commands> However, how do I go about doing... (1 Reply)
Discussion started by: rockysfr
1 Replies

9. Shell Programming and Scripting

concatenating strings..

hey guys.. probably a simple question but i cant seem to find any info on it. i have a small array of strings, and i want to concatenate the contents of the array into one big string. any ideas on how i can do this? cheers. (2 Replies)
Discussion started by: jt_csv
2 Replies

10. Shell Programming and Scripting

Concatenating Strings

Is there any function to concatenate strings in shell script (2 Replies)
Discussion started by: radhika03
2 Replies
Login or Register to Ask a Question