31

I tried 'man echo' in Bash and it told me that 'echo --help' will display help then exit, and similarly, that 'echo --version' will output version and exit. But why it doesn't work ? 'echo --help' just simply prints '--help' literally.

9
  • 2
    Note that production of a help message by echo --help is a bug. Imagine what happens if you do: printf "The option you entered is: " ; echo "$opt" and the shell variable opt happens to be contain the text --help. Per the POSIX standard, echo is not permitted to behave in this way, but the GNU echo is obnoxiously non-conforming. Commented Sep 5, 2014 at 5:23
  • 3
    @R.. So echo -e and echo -n is also a bug, by your logic?
    – Tyilo
    Commented Sep 10, 2014 at 21:44
  • @Tyilo: See the specification in POSIX, or my coverage of the issue here: etalabs.net/sh_tricks.html Commented Sep 10, 2014 at 22:04
  • @R.. Imagine what happens if you do: printf "The option you entered is: " ; echo "$opt" and the shell variable opt happens to be contain the text -e or -n.
    – Tyilo
    Commented Sep 11, 2014 at 8:58
  • @Tyilo: Rather than re-explaining it I liked to a page where I already explained the answer to that question. Commented Sep 11, 2014 at 13:17

5 Answers 5

42

man echo relates to the echo program. GNU echo supports a --help option, as do some others. When you run echo in Bash you instead get its builtin echo which doesn't.

To access the echo program, rather than the builtin, you can either give a path to it:

/bin/echo --help

or use Bash's enable command to disable the built-in version:

$ enable -n echo
$ echo --help

Bash has built-in versions of a lot of basic commands, because it's a little faster to do that, but you can always bypass them like this when you need to.

8
  • Serves me right for using zsh. enable does work, though. Commented Sep 4, 2014 at 3:39
  • 1
    @booksee: Yes, they can differ. GNU echo in particular has a lot of extensions above POSIX, and Bash's echo only a few. There isn't really a general name that I can think of for commands that could be shadowed by builtins. There aren't so many that you'd need one: echo, test, [, and pwd are the ones you're likely to encounter. Commented Sep 4, 2014 at 3:56
  • 2
    "use Bash's enable command to disable" yup, guys, is like that
    – Braiam
    Commented Sep 4, 2014 at 4:00
  • 1
    @boksee: in bash, use type -all echo to see which one is used (and what others there are visible, builtin/alias/function/in the path) Commented Sep 4, 2014 at 8:22
  • 1
    @MichaelHomer and @Gnouc: in bash command something bypasses and alias -or- function named "something", but not builtins. \something only bypass alias. so indeed enable -n something ; command something would ensure you call the one in the PATH (wherever it is, if any) Commented Sep 4, 2014 at 8:24
12

In addition to other answers, you can use

$ help echo

too see a brief help for built-in echo command.

9

You are using the echo built-in command of bash, which does not understand the --help option. Use:

/bin/echo --help

To see the help for the built-in echo, use man bash and search for echo.

1
  • 1
    this worked for me Commented Sep 22, 2018 at 23:05
1

Actually there are 2 echo program: echo (GNU Bash) and echo (GNU Coreutils).

echo (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 --> return error "--help"
echo --version --> return error "--version"
help echo

echo (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 --> return GNU Coreutils help
/bin/echo --version --> return GNU Coreutils version
help /bin/echo --> return error
0

I needed to write a tcsh shell script with echo -ne commands in it.

From /bin/echo --help:

For complete documentation, run: info coreutils 'echo invocation'

From info coreutils 'echo invocation':

Due to shell aliases and built-in echo command, using an unadorned echo interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., `env echo ...') to avoid interference from the shell.

So, the solution is to prepend env to your echo commands in the script. For example:

env echo -ne 'Hello\r'

You must log in to answer this question.

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