Link to home
Create AccountLog in
Avatar of ibanja
ibanja

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
It's your implementation of "echo"

Try echo -e

wmp
... 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!  :-)
Avatar of ibanja
ibanja

ASKER

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