0

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
aac
  • 55
  • 1
  • 6
  • As per the linked question, the version of (GNU) echo is found using /bin/echo --version, not /bin/echo -v – user4556274 Dec 27 '21 at 16:43
  • What doesn't work with the suggested solution to use the binary(!) and then --version? – FelixJN Dec 27 '21 at 16:43
  • @user4556274 Calling directly the binary combined with this option works – aac Dec 28 '21 at 09:04
  • I actually have an XY problem where I try to find why colors does not print on my server but prints locally. And one of my guess was the echo version. Which is not. So I will open a new question about the problem precisely – aac Dec 28 '21 at 09:17
  • Actually nevermind, printf is more coherent than echo 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

3 Answers3

2

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?

ilkkachu
  • 138,973
0

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. :)

Seamus
  • 2,925
0

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

  • to find out echo (GNU Coreutils) you could using this commands
    dpkg-query --listfiles coreutils | egrep '/bin/|/sbin/'
    man echo
    which echo
    
  • to run echo (GNU Coreutils) you should call /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
    
AdminBee
  • 22,803
YGautomo
  • 111
  • Not just those two, as likely each shell has its own implementation, plus then there's the non-GNU standalone implementations. On Linux, you're probably most likely to see GNU or Busybox, but of course the various BSDs and other OSes also have their own implementations. – ilkkachu Oct 17 '23 at 14:10
  • Noted. Sorry, i forgot to add, currently i'm using Ubuntu. I didn't know other implementation in different shell & distro. – YGautomo Oct 17 '23 at 14:51