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
5
  • As per the linked question, the version of (GNU) echo is found using /bin/echo --version, not /bin/echo -v Dec 27, 2021 at 16:43
  • What doesn't work with the suggested solution to use the binary(!) and then --version?
    – FelixJN
    Dec 27, 2021 at 16:43
  • @user4556274 Calling directly the binary combined with this option works
    – aac
    Dec 28, 2021 at 9: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, 2021 at 9: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, 2021 at 10:08

3 Answers 3

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?

5
  • I replaced echo -n with printf "%b" and echo with printf "%b\n" and it works same as echo
    – aac
    Dec 28, 2021 at 9:14
  • @user4556274 gave the right solution, but he didn't created an answer. So maybe you could edit your one to suggest to use --version with the binary path instead of just -v. Or maybe he could
    – aac
    Dec 28, 2021 at 9:41
  • It's the other way round. non-XSI echos 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). Oct 27, 2023 at 14:21
  • Note that the echo from GNU coreutils outputs --version<newline> as required by POSIX when POSIXLY_CORRECT is in the environment. Oct 27, 2023 at 14:23
  • toybox 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. Oct 27, 2023 at 14:35
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. :)

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
    
2
  • 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, 2023 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, 2023 at 14:51

You must log in to answer this question.

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