1

While looking for the plain truth on echo I found this page:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

It's normally a HTML frame on this site https://pubs.opengroup.org/onlinepubs/9699919799/ (where you can search for "echo").

This claims to be POSIX, but I see no -n and I see \c instead!

What have I found?

GracefulRestart points out that /bin/echo recognises \c but it doesn't do that by default: I must do echo -e for \c to be recognised.

8
  • 1
    On your system, run the command type -a echo. Usually, you will have a shell built-in called echo provided by your shell in addition to /usr/bin/echo which would have the flags you are looking for. The shell built-in usually supersedes the installed program. Commented Feb 4, 2020 at 20:44
  • not here, I have the gnu coreutils 8.3 echo as /bin/echo it recognises -n and requires -e to regognise \c $ /bin/echo --version echo (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc.
    – Jasen
    Commented Feb 4, 2020 at 20:53
  • 1
    Does "This claims to be POSIX, but..." imply that you believe that that is not the POSIX specification for echo? Since you are mentioning GNU: notes about POSIX compatibility can be found in info coreutils echo; they are not included in man echo.
    – fra-san
    Commented Feb 4, 2020 at 20:59
  • I found a web site on the internet but wasn't sure if it was legit... info says If the ‘POSIXLY_CORRECT’ environment variable is set,... I think that answers my question.
    – Jasen
    Commented Feb 4, 2020 at 21:02
  • If you want to write things to the screen in a portable manner, use printf instead of echo. Commented Feb 4, 2020 at 21:09

2 Answers 2

6

You have found IEEE 1003.1-2017, a.k.a. the Single Unix Specification, published by The Open Group.

For more, see "What exactly is POSIX?", "Difference between POSIX, Single UNIX Specification, and Open Group Base Specifications?", and all of their linked questions and answers.

The -n is there, in boldface no less so it is hard to miss. And yes, \c is standard.

The variations in behaviour of echo are notorious. You should not be surprised that /bin/echo is not the same as a shell built-in echo, and that one requires an -e where another does not. It's not even that simple.

For a long explanation, see "Why is printf better than echo?".

For the little-known variability of printf, ironically involving the very same \c escape sequence, see "Bash printf formating not working".

1
  • To make it easier to understand: linux and gnu tools are mostly POSIX compliant, but for a UNIX branding, you need to implement all XSI enhancements. This is where Linux is far from being standard compliant. dash e.g. does not implement support for multibyte characters.
    – schily
    Commented Feb 11, 2020 at 13:11
-2

If you were on a POSIX certified platform with UNIX branding, echo did handle \c by default.

This applies to Solaris, AIX, HP-UX and Apple.

Apple even uses bash to support this echo behavior, but you need a specific compile option to make bash compliant by default. This compile option for bash is used for MacOS and for Solaris. So peole may be confused when they realise that bash behaves different on a POSIX certified platform,

Remember: GNU reads GNU is not UNIX.... many GNU tools are not POSIX compliant or not POSIX compliant by default. So be careful when you are not on a certified platform.

BTW: You may get the POSIX branding for small embedded platforms even when you do not support echo \c, but for a UNIX branding, you need to support all XSI extensions to POSIX. Linux does not support most of the XSI extensions. Many Linux distros e.g. use dash as the default shell and dash id definitely not compliant, e.g. because it does not support multi byte characters.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .