4 ms·
> echo is non portable and its behaviour diverges between systems and shell builtins `echo` is portable: https://pubs.opengroup.org/onlinepubs/009695399/utilit
by uplime 5y ago
> echo is non portable and its behaviour diverges between systems and shell builtins
`echo` is portable: https://pubs.opengroup.org/onlinepubs/009695399/utilities/echo.html https://pubs.opengroup.org/onlinepubs/009695399/utilities/ec... Any options passed to `echo` are not. Although I agree `printf` should be used instead.
- chrisseaton 5y ago> Any options passed to `echo` are not Using common sense, this is probably what they mean by 'non portable' isn't it?
- uplime 5y agoIt probably is what they meant. However, what was written is that `echo` itself is non-portable, which isn't correct.
- tssva 5y agoThe context of the article is the Gnu coreutils. The coreutils version of echo accepts options which is not allowed according to the link you referenced. Therefore within the context of the article stating echo is non-portable is correct.
- uplime 5y agoGNU coreutils isn't a standard. It conforms to and extends the POSIX standard. If it was just talking about GNU echo, then that `echo` would work the same on every machine. It even explicitly mentions other `echo` implementations, like the shell builtins. `echo` itself is portable, none of its flags are.
- burntsushi 5y agoYou seem to have a very narrow definition of "portable." Can you link to the definition of the jargon term you appear to be using here? Also, have you considered that some words have colloquial meanings too, and that it doesn't make sense to label them as "not correct"?
- uplime 5y ago> You seem to have a very narrow definition of "portable." Can you explain how wanting to include more programs into the definition of portable is narrow? > Can you link to the definition of the jargon term you appear to be using here? The same definition I've always seen in every community discussing shell-scripting: if it's specified by the POSIX (Portable Operating System Interface) standard. POSIX does have a definition of `echo`. GNU echo does not conform to it by default, but can be configured to. > Also, have you considered that some words have colloquial meanings too, and that it doesn't make sense to label them as "not correct"? But it makes sense to label `echo` itself as not portable? The argument can be made that `GNU echo` is not portable (even though it can be with an environment variable), however that should be specified, instead of mentioning there are several implementations where it is non-standard.
- burntsushi 5y agoYou sound like a zealot to be honest. You misconstrued my post and gave a disingenuous response IMO. And you didn't answer my questions.
- kevincox 5y agoDoesn't that make echo non-portable in practice. `echo -e` should print "-e\n" according to POSIX but the most common implementation will print "". As far as I can tell there is no way to work around this.
- uplime 5y agoIt makes relying on meaningful options passed to `echo` non-portable. You can always rely on `echo` being on the system (thanks to POSIX), which is what makes it portable. Relying on the behavior of -e/-n/etc is not.
- kevincox 5y agoBut that is my point. Print the string "-e\n" in a portable way. I can't think of a way. For GNU coreutils you would need to do `echo - -e` but on a complaint echo you would need to do `echo -e`. So maybe it is portable for some strings. But it can't be used for all cases. Even something as simple as outputting a user entered string can't be done because you don't know if it is handled specially.
- uplime 5y agoYou can with the POSIXLY_CORRECT environment variable: $ /bin/echo --version echo (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Brian Fox and Chet Ramey. $ POSIXLY_CORRECT=1 /bin/echo -e -e Which is why I say relying on `echo` is fine, since its standard. Relying on `echo` to use options is not. That's also why the author is correct that you should use `printf`, because POSIXLY_CORRECT is not set, then you can run into non-standard behavior, like you showed.