2

I have an echo statement in my script as below:

echo -ne "Check Script";

I was expecting it to print

Check Script

but I am getting the below output

-ne Check Script

But when I run the same script on some other machine I get the expected output. What could be machine specific variables or properties because of which the script is behaving differently.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
g4ur4v
  • 1,724
  • 8
  • 27
  • 34
  • 1
    The operating system, the shell, and the value of `shopt xpg_echo`. – Mikel Nov 07 '12 at 16:26
  • @Mikel: Red Hat Enterprise Linux Server release 5.7 (Tikanga). Using kshell. shopt xpg_echo didn't work for me.I got error -ksh: shopt: not found [No such file or directory] – g4ur4v Nov 07 '12 at 19:39

1 Answers1

8

That's the behavior of POSIX and UNIX conformant echo. With a UNIX conformant echo, you'd write:

echo 'Check Script\c'

Best is not to use echo but printf instead which has fewer portability issues.

printf %s 'Check Script'

Note that POSIX allows -n as an extension (but with unspecified behavior). echo -e is meant to output -e\n, so in that regard, bash and zsh are not POSIX conformant.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • For a longer answer by M. Chazelas, see https://unix.stackexchange.com/a/65819/5132 . (-: – JdeBP Aug 25 '18 at 20:53