In the line of https://stackoverflow.com/questions/906590/echo-version-doesnt-works I need to know the version of echo
$ type -a echo | cut -d " " -f 3- | xargs -d '\n'
a shell builtin /usr/bin/echo /bin/echo
$ /usr/bin/echo -v
-v
$ /bin/echo -v
-v
In the line of https://stackoverflow.com/questions/906590/echo-version-doesnt-works I need to know the version of echo
$ type -a echo | cut -d " " -f 3- | xargs -d '\n'
a shell builtin /usr/bin/echo /bin/echo
$ /usr/bin/echo -v
-v
$ /bin/echo -v
-v
Standard echo
doesn't take any options (or with XSI extensions, only recognizes the -n
argument). In many cases, the echo
you use is one built in to the shell, and in none of the shells I tested, the builtin echo
recognizes --version
. They do variously recognize at least -e
, -E
, -n
though. The only echo
I found that recognizes --version
is the one from GNU coreutils, i.e. the one installed at /bin/echo
on many Linux systems.
So, in most cases, the question of recognizing the version of echo
comes down to recognizing the shell you're running. Or running /bin/echo
, if you know you're on a system where that's GNU, and not e.g. Busybox. Also, as seen on the man page, the GNU coreutils version only supports --version
, not -v
or -V
.
But in general, if you're trying to get something done, you don't need to depend on the particular version of something, just the supported functionality. And when looked at that way, it's easier to just use printf
rather than echo
.
If you want to print a string as-is, use
printf "%s\n" "$var"
If you want to process backslash-escapes in the string, use
printf "%b\n" "$var"
And if you want to skip the final newline, remove the \n
from the first argument.
See Why is printf better than echo? and How do I print '-e' with echo?
echo -n
with printf "%b"
and echo
with printf "%b\n"
and it works same as echo
– aac
Dec 28 '21 at 09:14
--version
with the binary path instead of just -v
. Or maybe he could
– aac
Dec 28 '21 at 09:41
echo
s are not guaranteed not to support options, XSI ones are. That is a Unix compliant echo
is required not to support option, while POSIX-only ones may support -n
(will be extended to -e
/-n
/-E
and combinations thereof in the next version of the standard).
– Stéphane Chazelas
Oct 27 '23 at 14:21
echo
from GNU coreutils outputs --version<newline>
as required by POSIX when POSIXLY_CORRECT
is in the environment.
– Stéphane Chazelas
Oct 27 '23 at 14:23
echo
(Android) also support --version
(and --help
and --
, I'll update that Q&A you linked). The echo
builtin of the Android shell (mksh) doesn't.
– Stéphane Chazelas
Oct 27 '23 at 14:35
Springboarding off @ilkkachu's answer:
On my macOS 10.15 laptop using zsh
, I get the following:
% which echo
echo: shell built-in command
% whereis echo
/bin/echo
% /bin/echo --version
--version
Which is explained by referring to man echo
: Apple packages a 2002 BSD version of echo
in their new laptops - it supports only the -n
argument. :)
I think there are 2 echo
programs: from GNU Bash and from GNU Coreutils.
echo
from GNU Bash
to find out echo
(GNU Bash) you could using this commands help
or help echo
to run echo
(GNU Bash) just try using echo
echo "Hello World"
echo -e "line 1\ttab\nline2"
echo --help # will return error "--help"
echo --version # will return error "--version"
help echo
to know the Bash version try bash --version
, there is no echo GNU Bash version
echo
from GNU Coreutils
dpkg-query --listfiles coreutils | egrep '/bin/|/sbin/'
man echo
which echo
/bin/echo
/bin/echo "Hello World"
/bin/echo -e "line 1\ttab\nline2"
/bin/echo --help # will return GNU Coreutils help
/bin/echo --version # will return GNU Coreutils version
help /bin/echo # will return error
/bin/echo --version
, not/bin/echo -v
– user4556274 Dec 27 '21 at 16:43--version
? – FelixJN Dec 27 '21 at 16:43printf
is more coherent thanecho
so now it prints color wherever I execute it. So if someone has the idea to go here for this problem: Heres your solution – aac Dec 28 '21 at 10:08