1

I noticed that command like cd .. is actually converted to builtin cd .. and then executed. But how is builtin defined? which builtin doesn't show anything and it's not in /bin, /usr/bin etc. Is it just part of the bash program and is being handled differently?

h__
  • 275
  • 4
  • 12

1 Answers1

4

builtins are just part of bash. You can find out if a command is builtin by using type. For example:

$ type export cd source alias
export is a shell builtin
cd is a shell builtin
source is a shell builtin
alias is a shell builtin

Many builtins commands are builtin because they can work no other way. cd and source are examples.

Other builtins are builtin merely for efficiency. test (AKA [...]) and echo are examples.

To find out more, see the SHELL BUILTIN COMMANDS section of man bash or the online bash manual. To get a list of available builtins, run help at the command prompt. To get information on a particular builtin, say test, run help test. Note that help test will provide information on bash's test builtin while, by contrast, man test will likely provide help on the external test command.

Which commands are available both as executable and builtin

With the -a option, type will display all of the places that contain the named command. For example:

$ type -a echo
echo is a shell builtin
echo is /bin/echo

How to use an executable when a builtin is available

If, for some odd reason, you want to use a particular executable and not the shell builtin, all you have to do is specify the path:

$ /bin/echo "This is not the builtin"
This is not the builtin

Since the builtins typically have more features, this is typically only useful for compatibility testing.

How to execute the shell builtin when it is hidden by a function or alias

Given a choice, the shell will normally choose to execute the builtin command. The exception is if the user has defined an alias or function of the same name. If you want to be sure that you are executing the builtin, use the builtin command:

$ builtin echo  1 2 3
1 2 3
John1024
  • 16,593
  • 5
  • 50
  • 45
  • So, `builtin` is defined in the source of bash? – h__ Mar 16 '15 at 01:53
  • @hyh Yes, they are defined in the source of bash. – John1024 Mar 16 '15 at 01:55
  • 2
    Also `help` is useful to extract that portion of the man page: `help builtin`. I have long ago stopped using `which` in favour of `type -a` – glenn jackman Mar 16 '15 at 02:14
  • @glennjackman I updated the answer to discuss `type -a` in addition to the `type`. A command to extract relevant portions of the lengthy bash manpage would be helpful. On my machine, though, `help builtin` provides info on the command called `builtin` (now added to the answer), not on the general concept of shell builtins. – John1024 Mar 16 '15 at 02:29
  • `help` is key here. You can run `help` by itself to get a list of builtins or `help [name of builtin]` for help with a particular builtin. – Eric Aug 25 '16 at 00:03
  • agreed : built-ins are defined by bash's source code. e.g., consider the test command. You may have a man page for "test" (or "`[`"), referring to an actual executable file that could be run, and which some shells use. (Example: [OpenBSD's man page for test](http://apple.stackexchange.com) .) But if you run `man $SHELL` (which will typically show bash's man page if you are using bash), you may find you are looking at a different manual which may describe a whole lot more details, including a built-in "test" command that overrides the external test executable file. – TOOGAM Aug 25 '16 at 03:19