We help IT Professionals succeed at work.

Why doesn't \n work in centos script

I have the following script:
$ cat ./test.sh
#!/bin/sh
var1="line1\nline2\n"
echo $var1

Open in new window


In Ubuntu I get the following when run:
$ ./test.sh
line1
line2

Open in new window


In Centos I get the following when run:
$ ./test.sh
line1\nline2\n

Open in new window


Why is it behaving differently?  I have tried replacing the:
#!/bin/sh
with:
#!/bin/bash

but no change.

Any insights appreciated.

Thanks,
Frank
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Use option -e

echo -e  $var1
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
It's your implementation of "echo"

Try echo -e

wmp
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
... or use ksh
The problem is with the echo command.  On CentOS it won't interpret the \n unless you use
echo -e $var1
Damn, beaten to it!  :-)

Author

Commented:
Thanks everyone.  Since farzanj got there first I gave him the points, but appreciate the responses.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.