Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Script - Disappearing NewLine

Status
Not open for further replies.

miblo

Technical User
Sep 3, 2001
76
SE
Folks,

I have an issue with disappearing newlines, when performing commands in bash scripts or at prompt.

Basically, when a command is executed and returns several lines, each line ends with a <new line> character. But, when the same command is executed and the results are placed in an environment variable, the <new line> characters are stripped.

And I obviously want to retain the <new line> characters in the variable.

By example:

[mibl@a51 mibl]$ who
mibl pts/0 Sep 15 15:19 (my.example.com)
mibl pts/1 Sep 15 08:56 (my.example.com)
mibl pts/2 Sep 15 17:13 (my.example.com)


[mibl@a51 mibl]$ X=`who`; echo $X
mibl pts/0 Sep 15 15:19 (my.example.com) mibl pts/1 Sep 15 08:56 (my.example.com) mibl pts/2 Sep 15 17:13 (my.example.com)


As can be seen, the variable $X does not contain the output from the who command with <new line> characters.

Why is this? And how can this be solved?

Running Bash 2.05a, Linux RH9.

TIA

~Mike
 
X=`who`;echo &quot;$X\n&quot;

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Try this:
Code:
[mibl@a51 mibl]$ X=`who`; echo &quot;$X&quot;
#                              ^  ^
This is the echo command that converts any IFS char to single space

Hope This Help
PH.
 
Thanks, problem solved.

Apparently enclosing the variable with double-quotes solves my problem.

Thanks,

~Mike





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top