skip to Main Content

I am using a Bash shell on Ubuntu 22.04.

When I run type -a echo, I get the following output:

enter image description here

What I understand is that when I run a command, let’s say echo "Hello Wolrd!", Bash will run the built-in echo command. Am I right?

My first question is: If Bash runs the built-in echo command by default, why there are standalone echo programs: /usr/bin/echo and /bin/echo? Are there specific scenarios when one would prefer to use the standalone programs of echo?

My second question is: Why there are two standalone programs of echo, as shown in the screenshot above? And if I decide to run the standalone program, which one should I use and why?

I would appreciate a detailed answer. Thanks.

Edit-1
This is what I get when I run the readlink command to check whether /bin is a symlink to /usr/bin:

enter image description here

2

Answers


  1. Am I right?

    Yes.

    why there are standalone echo programs

    Because you might want to use them.

    Are there specific scenarios when one would prefer to use the standalone programs of echo?

    Yes, when you fork+exec and want to run echo, not bash. Like xargs echo given in comments. Note that xargs with no arguments by default runs echo, so kind of xargs depends on existence of echo executable.

    Why there are two standalone programs of echo, as shown in the screenshot above?

    On most systems, /bin is a symlink to /usr/bin or otherwise. It is the same file.

    which one should I use and why?

    Use the one first in PATH. Because PATH is for choosing what to run.

    Login or Signup to reply.
  2. Bash has only a built-in echo. The /bin/echo program in your system is not coming from the Bash package/project.

    Bash’s echo is only available from within bash. We cannot get the operating system to execute an echo program, for instance with the C library execl function:

    Without a /bin/echo, for instance, this cannot work:

    execl("/bin/echo", "echo", "foo", (char *) NULL);
    

    Only this:

    execl("/bin/sh", "sh", "-c", "echo foo", (char *) NULL);
    

    But, why would you ever need to exec /bin/echo, instead of just using the I/O library of the programming language to dump something to standard output? That’s a good question: you wouldn’t. The specific example of /bin/echo is not a useful program to have other than as a shell built-in. And even that is mostly deprecated.

    The main reason why there is a real echo command is that, well, POSIX, ahem, echoes my remarks above about exec. See section 1.6 here where this requirement is given:

    However, all of the standard utilities, including the regular built-ins in the table, but not the special built-ins described in Special Built-In Utilities, shall be implemented in a manner so that they can be accessed via the exec family of functions as defined in the System Interfaces volume of POSIX.1-2008 and can be invoked directly by those standard utilities that require it (env, find, nice, nohup, time, xargs).

    This also provides the rationale we are looking for: there are certain utilities which invoke other utilities, like xargs and find, without the use of a shell: just by the exec mechanism. They cannot work with utilities that are built-in only.

    In the specific case of echo, using it with any of the above commands is not very useful; but POSIX doesn’t make an exception for the echo utility; if it is offered as a built-in, it must still be also provided in exec-able form.

    The only utilities that are allowed to exist as built-ins only are those that must necessarily be built-in in order to make sense. And not even all of those, but a subset. For instance cd doesn’t make sense as an external command, since it doesn’t change the directory of the process which executes it; at best it provides an error check whether it is able to change directory. Yet, cd is not one of the Special Built-In Utilities that can be provided only as built-in utilities; a POSIX system must have an executable cd utility.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search