New line in Echo command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers New line in Echo command
# 1  
Old 10-31-2013
New line in Echo command

Hi ,

This might sound little familar to you guys, but am struck with something.

i have an IF conditioned loop which will determine a variable to get used in the later part of the script.
Code:
if [ blah = blah ];
then
     if [ blah1=blah ];
     then
        Var_name="This is first line.This is second line.This is third line"

Now, when am using this Var_name in the later part of the script as mentioned below, its appearing as a whole line.

Code:
echo "For testing purpose ${Var_name}"

Please let me know what am doing here.

Note : I tried using single commands of echo -e "\n" in a shell script, its working fine. But when i tried to execute in a 50 lines shellscript its not working.
I tried to search in the forum ,for solutions, but am not able to find it. Hence , am posting a new thread.

Thanks,
P

Last edited by Scott; 10-31-2013 at 01:40 PM.. Reason: Please use code tags
# 2  
Old 10-31-2013
The simplest way is to actually use newlines...
Code:
Var_name="This is first line.
This is second line.
This is third line."

... remembering, of course, to quote the variable when used to preserve them in the output...
Code:
$ echo "$Var_name"
This is first line.
This is second line.
This is third line.

$ echo $Var_name
This is first line. This is second line. This is third line.

These 2 Users Gave Thanks to Scott For This Post:
# 3  
Old 11-01-2013
Thanks Scott for your quick and prompt reply.

This method i have tried, but its not working as expected.
Let me give the complete overview of it ,
The Var_name is used as an variable to display the values in the HTML format table cell in mail as mentioned below,
Code:
<td ><b>Variable is :</b></td><td>"${Var_name}"</td>

Its not appearing as
Code:
"This is first line. 
This is second line. 
This is third line."

Could you please suggest here ?

Last edited by Scott; 11-01-2013 at 06:46 AM.. Reason: Code tags
# 4  
Old 11-01-2013
Then it is an HTML issue.
Try the HTML tag <br>
Code:
Var_name="This is first line.<br>
This is second line.<br>
This is third line."

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 11-01-2013
Wow !! that works like Charm !!! thanks alot.

Now, in the same script, am using the same variable Var_name in an sed function to be parsed to replace a value with the variable value.
Original code :
Code:
sed 's/oldvalue/'"${Var_name}"'/g'

and its failing to get replaced,
Sed: Function s/oldvalue/##This is First Line. <br> cannot be parsed.

Is there any other options which we can do to get sorted.

Thanks in advance.
P
# 6  
Old 11-01-2013
MadeInGermany is correct, but you probably want to know why:

If you check the HTML source code you produce you will see that indeed there are three lines as Scott said and suggested. HTML source code, though, is not displayed as it is when you display it using a web browser. The browser interprets it and part of this interpretation is to "interpret away" the line breaks.

MadeInGermany just put the HTML equivalent of line breaks (the "<br />" tags) into the text, which will - when interpreted by a web browser - produce line breaks. Notice, that your text will, when showed in a web browser, look the same if you write:

Code:
Var_name="This is first line.<br />
This is second line.<br />
This is third line."

or

Code:
Var_name="This is first line.<br />This is second line.<br />This is third line."

because the "away-interpretation" of real line breaks works both ways: the browser simply ignores them and it will also ignore their absence.

I hope this helps.

bakunin

/PS: remove the (real) line breaks from your variables content, the browser would ignore them anyway. Their existence is probably the reason why your sed-statement is failing: web browsers ignore line breaks, but sed does not.

Last edited by bakunin; 11-01-2013 at 05:49 AM..
This User Gave Thanks to bakunin For This Post:
# 7  
Old 11-01-2013
Thanks bakunin, that was an good explanation.

And yes i understand that because of the <br/> only my sed is not functioning.

But, am just looking for the options for both of them to work , is there any ?

If not will have to figure out assigning different variables but that's not feasible.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

2. Shell Programming and Scripting

Need to echo command successful if command is executed successfully

Hello, I have written a command n shell script : srvctl relocate service -d t1 -s s1 -i i1 -t t1 -f If the above command executes successfully without error I need to echo "Service relocated successfully and If it errors out I need to trap the errors in a file and also need to make... (1 Reply)
Discussion started by: Vishal_dba
1 Replies

3. Shell Programming and Scripting

How to read in a part of an echo line?

Hello, I have a very basic script #!/usr/bin/ksh while print ' ' print ' 1. View Command History ' print ' 2. List files in current Directory ' read opt'?Enter Option> ' ;do if ;then fc -l fi # if ;then ls -la I want to... (10 Replies)
Discussion started by: Grueben
10 Replies

4. Shell Programming and Scripting

echo two command output in the same line

Hi, I try to write script and echo two command at the same line . echo "A" echo "B" How can I pipe above two command at the same line in text file . So, in the output text file , you can see below ??? A B not A B Any sugggestion ??? (4 Replies)
Discussion started by: chuikingman
4 Replies

5. Shell Programming and Scripting

Echo and a command's output on the same line

Hello, I'm writing some bash scripts and I'm trying to get an echo command and the output of another command to display on the same line. For example: I want to run echo "Operating System: " unameand have it displayed as Operating System: Darwin Thanks for your help! (7 Replies)
Discussion started by: codyhazelwood
7 Replies

6. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

7. Shell Programming and Scripting

new line in echo

Hi i would like disply the new line in echo command. i have input like: echo " X1 02:12:13 X2 03:02:12 " out put: X1 02:12:13 X2 03:02:12 can you tell how can use new line option in echo command. (5 Replies)
Discussion started by: koti_rama
5 Replies

8. UNIX for Advanced & Expert Users

echo and new line

hi I want to display: The students are: a b c What I get from echo "The students are:" "$list" is The students are: a b c any idea? (6 Replies)
Discussion started by: melanie_pfefer
6 Replies

9. Shell Programming and Scripting

Output of both the echo statement in one line

I have script like echo -n FINISHED FEXP: ${TABLE2EXP} echo $STATUS I want the output of both the echo statement in one line How can i do this (3 Replies)
Discussion started by: scorp_rahul23
3 Replies

10. Shell Programming and Scripting

how to echo the file contents LINE BY LINE

hello, i have a listing (let say ABC) consists of the below: : public database link public synonym role rollback segment : when i run the below for loop, for i in `more ABC` do echo "$i" done it gives me, : public database (4 Replies)
Discussion started by: newbie168
4 Replies
Login or Register to Ask a Question