Question

I was wondering about tools that are built into the bash shell. For example, type pwd tells me that pwd is built into the shell.

whereis pwd
/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz

aptitude search pwd

does not (seem to) give anything on the pwd I use. (I'm on a Debian system.)

-- Is there any way to find out what stuff are built in? Besides brute force with type, that is.

-- Is the pwd in the bin folder (above) the same pwd that is built in? Is it loaded into the shell at initiation? Or is it executed from that folder by the shell? If so, in what way is it built-in?

-- Why are stuff built in in the first place? Are they especially tweaked to fit the shell, or is it just so that they can be invoked internally so they don't require a new process? I managed to catch a pwd with pwd & and ps. Is this a circumvention or are they separate processes?

-- Feel free to tell me anything else on the topic :)

Was it helpful?

Solution

Is there any way to find out what stuff are built in?

help will get you a complete list. You can run help with a builtin command as argument to get more detailed information. info "(bash) Shell Builtin Commands" will display the Bash manual for all the builtins.

Is the pwd in the bin folder (above) the same pwd that is built in?

No, they are completely different:

$ builtin pwd --help
bash: pwd: --: invalid option
pwd: usage: pwd [-LP]

$ /bin/pwd --help
Usage: /bin/pwd [OPTION]...
Print the full filename of the current working directory.

  -L, --logical   use PWD from environment, even if it contains symlinks
  -P, --physical  avoid all symlinks
      --help     display this help and exit
      --version  output version information and exit

NOTE: your shell may have its own version of pwd, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

Report bugs to <bug-coreutils@gnu.org>.

Why are stuff built in in the first place? Are they especially tweaked to fit the shell, or is it just so that they can be invoked internally so they don't require a new process?

From the manual: "Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities." It would be hard to make a command like cd work externally because it affects the state of the shell. Of course, it is easy to duplicate the behavior of pwd or true, but the POSIX standard requires that they are built-ins.

I managed to catch a pwd with pwd & and ps. Is this a circumvention or are they separate processes?

Running builtin & will cause Bash to run a subshell in the background. You can see this easily by doing read &, since read waits until it has input.

OTHER TIPS

To answer your first question, I found that if I type (in my bash shell) "builtin" and then tab-tab, it shows me a list of the builtins, since it has tab-completion. I know it's only a small part of what you're asking, but it's a start, since I don't know all the "why" stuff. :P

Shell builtins are obviously more efficient than separate binaries. The separate binaries are totally independent and are basically for use with other shells that do not have this stuff builtin. You can force bash to use binary by putting the command in quotes, IIRC. If you man bash you will find quite some information on builtin commands and how exactly they work (it's not always the same like external binaries).

The bash manual page (man bash) enumerates the built-ins.

whichcommand will list where non-built-ins are found, taking into account ${PATH} and the like.

The files in /bin/ are replacements, in case you use a shell that doesn't have them as builtins.

They're built in for efficiency, to avoid a fork/exec, but bash in particular is usually smart enough to still fork when necessary (e.g. for | or & operations)

If you do need to run the versions in bin, you can invoke them by full pathname (/bin/pwd for example). (This also circumvents alias and the like.) — This is rarely useful with bash, but if you're using a very simple embedded shell, e.g. busybox, this can be helpful, as their builtins are often subsets of the functionality.

For a complete list of the shell builtins, use man bash. A builtin is a command that the shell has compiled inside the actual shell executable. That way, if for some reason you don't have the echo command, you could still run it from the shell. This is handy for stripped down systems(such as embedded devices), or cases where you have accidentally erased sections of the harddrive.

Usually, the builtin is more limited than the external executable, but it also does not require firing off another process, which you may not have enough resources to do. Again, most people don't run into that, but embedded systems and error recovery find it useful.

man builtin will show you which commands are built-in, and how they are executed. From the man page:

Shell builtin commands are commands that can be executed within the running shell's process.

Also on that man page you can find out what other processes are built in. Use man pwd to find out your specific version of pwd.

The man pages are your friends :)

As stated in the bash main-page, you can use the help built-in to get info about all the bash built-ins. So typing:

help

should give you a complete list. The reason for built-ins is that they are more efficient since invoking them does not involves spanning a new process. If you don't want to use the built-in for a given command but rather the command itself you must specify the full path of the command to the shell. e.g.

/bin/echo

instead of just echo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top