Echo Command Doesn’t Accept Switches (echo -n or echo -e) – Solutions

echoshell

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.

Best Answer

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.

Related Question