I am using a Bash shell on Ubuntu 22.04.
When I run type -a echo
, I get the following output:
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
:
2
Answers
Yes.
Because you might want to use them.
Yes, when you
fork+exec
and want to runecho
, notbash
. Likexargs echo
given in comments. Note thatxargs
with no arguments by default runsecho
, so kind ofxargs
depends on existence ofecho
executable.On most systems,
/bin
is a symlink to/usr/bin
or otherwise. It is the same file.Use the one first in
PATH
. BecausePATH
is for choosing what to run.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 libraryexecl
function:Without a
/bin/echo
, for instance, this cannot work:Only this:
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:This also provides the rationale we are looking for: there are certain utilities which invoke other utilities, like
xargs
andfind
, without the use of a shell: just by theexec
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 theecho
utility; if it is offered as a built-in, it must still be also provided inexec
-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 executablecd
utility.