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

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
  • 17,343