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
  • 25
  • 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

2 Answers2

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
  • 133,243
  • 15
  • 236
  • 397
  • I replaced `echo -n` with `printf "%b"` and `echo` with `printf "%b\n"` and it works same as echo – aac Dec 28 '21 at 09: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 '21 at 09:41
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,522
  • 1
  • 16
  • 31