Alexander Kohler
Alexander Kohler

Reputation: 1967

Shell built-in execution

I'm a little confused about the definition of a shell built-in. According to the Bash Reference Manual, for any built-in, the shell executes the command directly, without invoking another program. What exactly does it mean that a "command is executed directly"? For example, let's look at the cd built-in:

$ which cd 
/usr/bin/cd
$ type cd 
cd is a shell builtin

Now, let's look at the non built-ingrep:

$ which grep 
/usr/bin/grep
$ type grep 
grep is /usr/bin/grep

It appears that cd is a separate binary. How is executing cd (or any other builtin) any different than executing grep (or any other non-builtin)?

Upvotes: 2

Views: 577

Answers (2)

reflective_mind
reflective_mind

Reputation: 1515

In addition to Fred's correct answer:

I understand your confusion about builtins and external commands. The cd command is actually good for a little demonstration.

For example on my system, the situation is the following:

$ which cd    # gives no output
$ whereis cd  # again, no path found
cd:
$ type -a cd  # verifies that it's a real builtin command
cd is a shell builtin

This clearly shows that a cd command in terms of an external program is not installed on my machine. In your case, the which cd shows that an additional cd command (separate program) is installed under /usr/bin/cd.

Sometimes it is not clear when you type cd whether you're going to invoke the builtin or external command. To make sure, you really invoke the builtin cd command, you can execute the following:

$ builtin cd

From the help page of the builtin cd command:

$ help builtin

builtin: builtin [shell-builtin [arg ...]] Execute shell builtins.

Execute SHELL-BUILTIN with arguments ARGs without performing command
lookup.  This is useful when you wish to reimplement a shell builtin
as a shell function, but need to execute the builtin within the function.

Now you can see how the builtin command can be used to enforce execution of a shell builtin command. This can be very helpful in special cases.

Furthermore, you can (it's actually obvious) expect better performance from builtin commands [source]:

In general, an external command in a script forks off a subprocess, [*] whereas a Bash builtin does not. For this reason, builtins execute more quickly and use fewer system resources than their external command equivalents.

Upvotes: 2

Fred
Fred

Reputation: 6995

Some commands exist as both builtins to the shell, and as separate programs. The which command (which is a separate program and not a builtin!) only finds separate programs, and will never bother with builtins. Try type readarray and which readarray to see what happens for commands that exist only as builtins.

It is important to note that the builtin version and the external program may well differ. In some cases, you may prefer relying on the builtin (i.e. if you know what shell your script will be used on, but are unsure which exact external version will execute), or the other way around.

You can call the external program by using its full path (this will bypass the builtin).

When calling an external program, the operating system will launch a separate process, whereas the builtin is part of the shell program itself, and will therefore have much, much lower overhead. In many cases this overhead is insignificant, but if you are going to be executing the command many times, it may turn out to have a significant impact on the performance of your script.

Upvotes: 5

Related Questions