17

I have just started using Linux and I am curious how shell built-in commands such as cd are defined.

Also, I'd appreciate if someone could explain how they are implemented and executed.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
user385201
  • 171
  • 1
  • 1
  • 3

6 Answers6

16

If you want to see how bash builtins are defined then you just need to look at Section 4 of The Bash Man Page.

If, however, you want to know how bash bultins are implemented, you'll need to look at the Bash source code because these commands are compiled into the bash executable.

One fast and easy way to see whether or not a command is a bash builtin is to use the help command. Example, help cd will show you how the bash builtin of 'cd' is defined. Similarly for help echo.

SiegeX
  • 135,741
  • 24
  • 144
  • 154
12

The actual set of built-ins varies from shell to shell. There are:

  • Special built-in utilities, which must be built-in, because they have some special properties
  • Regular built-in utilities, which are almost always built-in, because of the performance or other considerations
  • Any standard utility can be also built-in if a shell implementer wishes.

You can find out whether the utility is built in using the type command, which is supported by most shells (although its output is not standardized). An example from dash:

$ type ls
ls is /bin/ls
$ type cd
cd is a shell builtin
$ type exit
exit is a special shell builtin

Re cd utility, theoretically there's nothing preventing a shell implementer to implement it as external command. cd cannot change the shell's current directory directly, but, for instance, cd could communicate new directory to the shell process via a socket. But nobody does so because there's no point. Except very old shells (where there was not a notion of built-ins), where cd used some dirty system hack to do its job.

How is cd implemented inside the shell? The basic algorithm is described here. It can also do some work to support shell's extra features.

Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121
4

Manjari, Check the source code of bash shell from ftp://ftp.gnu.org/gnu/bash/bash-2.05b.tar.gz You will find that the definition of shell built-in commands in not in a separate binary executable but its within the shell binary itself (the name shell built-in clearly suggests this).

Hans Raj
  • 41
  • 1
3

Every Unix shell has at least some builtin commands. These builtin commands are part of the shell, and are implemented as part of the shell's source code. The shell recognizes that the command that it was asked to execute was one of its builtins, and it performs that action on its own, without calling out to a separate executable. Different shells have different builtins, though there will be a whole lot of overlap in the basic set.

Sometimes, builtins are builtin for performance reasons. In this case, there's often also a version of that command in $PATH (possibly with a different feature set, different set of recognized command line arguments, etc), but the shell decided to implement the command as a builtin as well so that it could save the work of spawning off a short-lived process to do some work that it could do itself. That's the case for bash and printf, for example:

$ type printf
printf is a shell builtin
$ which printf
/usr/bin/printf
$ printf
printf: usage: printf [-v var] format [arguments]
$ /usr/bin/printf
/usr/bin/printf: missing operand
Try `/usr/bin/printf --help' for more information.

Note that in the above example, printf is both a shell builtin (implemented as part of bash itself), as well as an external command (located at /usr/bin/printf). Note that they behave differently as well - when called with no arguments, the builtin version and the command version print different error messages. Note also the -v var option (store the results of this printf into a shell variable named var) can only be done as part of the shell - subprocesses like /usr/bin/printf have no access to the variables of the shell that executed them.

And that brings us to the 2nd part of the story: some commands are builtin because they need to be. Some commands, like chmod, are thin wrappers around system calls. When you run /bin/chmod 777 foo, the shell forks, execs /bin/chmod (passing "777" and "foo") as arguments, and the new chmod process runs the C code chmod("foo", 777); and then returns control to the shell. This wouldn't work for the cd command, though. Even though cd looks like the same case as chmod, it has to behave differently: if the shell spawned another process to execute the chdir system call, it would change the directory only for that newly spawned process, not the shell. Then, when the process returned, the shell would be left sitting in the same directory as it had been in all along - therefore cd needs to be implemented as a shell builtin.

godlygeek
  • 1,489
  • 1
  • 8
  • 5
1

A Shell builtin -- http://linux.about.com/library/cmd/blcmdl1_builtin.htm for eg. -

which cd 
/usr/bin/which: no cd in (/usr/bin:/usr/local/bin......

Not a shell builtin but a binary.

which ls
/bin/ls
0

http://ss64.com/bash/ this will help you.

and here is shell scripting guide

http://www.freeos.com/guides/lsst/

Badr
  • 10,384
  • 15
  • 70
  • 104