A builtin command is a Linux/Unix command which is "built into a shell interpreter such as sh, ksh, bash, dash, csh etc". Thats where the name came from for these built-in commands. In other words we can say that these commands will always available in RAM so that accessing them is bit fast when compared to external commands which are stored on hard disk. There are many advantages for this type of commands. Below are some advantages.
Increase performance
When we keep frequently execute commands in RAM they will decrease time delay in accessing them from disks and improve performance. Built-in commands will solve this problem by loading them self into RAM when an Interpreter is loaded in to
Maintain set of commands at minimal shell
Sometimes there is high chance of system crash and you can not access anything on a machine. But if we still have shell access we can execute all builtin commands to do basic troubleshooting and recovery system.
How to list all built-in commands?
There are many ways we can list builtin commands. Commands like compgen and help will help us to list all available builtin commands for a particular shell.
Example:
[surendra@linuxnix.com ~]$ compgen -b
Output:
.
:
[
alias
bg
bind
break
builtin
caller
cd
command
compgen
complete
compopt
continue
declare
dirs
disown
echo
enable
eval
exec
exit
export
false
fc
fg
getopts
hash
help
history
jobs
kill
let
local
logout
mapfile
popd
printf
pushd
pwd
read
readarray
readonly
return
set
shift
shopt
source
suspend
test
times
trap
true
type
typeset
ulimit
umask
unalias
unset
wait
[surendra@linuxnix.com ~]$
or
[surendra@linuxnix.com ~]$ help
We should make note that "builtin commands varies from shell to shell".
How to check if a Linux command is built-in or not?
To check if a command is external or internal command(built-in) we have to execute type command
Example:
[surendra@linuxnix.com ~]$ type cd
Output:
cd is a shell builtin
Other examples:
[surendra@linuxnix.com ~]$ type pwd
pwd is a shell builtin
[surendra@linuxnix.com ~]$ type ls
ls is aliased to `ls –color=auto'
[surendra@linuxnix.com ~]$ type whoami
whoami is /usr/bin/whoami
If you observe ls and whoami commands are not built-in commands.
How to find Linux built-in command location?
This is bit tricky questions. As said earlier builtin commands are built into the shell and they are not individual files which can be located in the system. So there is no location for built in commands and this is the reason you will get below error for some of the built-in commands when you try to locate them.
[surendra@linuxnix.com ~]$ which cd
/usr/bin/which: no cd in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/visplad/bin)
[surendra@linuxnix.com ~]$ which let
/usr/bin/which: no let in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/visplad/bin)
How to find size of built-in commands?
Again this is bit tricky. We can not find the size of individual built-in commands as they are part and parcel of your shell. To know the size of your shell just execute below command
[surendra@linuxnix.com ~]$ du -hs $(which bash)
884K /bin/bash
[surendra@linuxnix.com ~]$
Replace bash with your desired shell.
How to get help on built-in commands?
So the important question is how to get help on these built-in commands. When you try to execute man on these commands you will get below error
surendra@linuxnix.com:~$ man cd
No manual entry for cd
surendra@linuxnix.com:~$
Then where we can get help on them?
There is special command to help you on this. The command to get more help on Linux/Unix built-in commands is help. To know about a command in detail just execute below command.
help built-in_command
Example
help cd
or
help -m cd
Output:
[surendra@linuxnix.com ~]$ help -m cd
NAME
cd – Change the shell working directory.
SYNOPSIS
cd [-L|-P] [dir]
DESCRIPTION
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
it’s value is used for DIR.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
The default is to follow symbolic links, as if `-L' were specified.
Exit Status:
Returns 0 if the directory is changed; non-zero otherwise.
SEE ALSO
bash(1)
IMPLEMENTATION
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
To get single line answer use -d option with help command.
[surendra@linuxnix.com ~]$ help -d cd
cd – Change the shell working directory.
To get simple command options use -s option.
[surendra@linuxnix.com ~]$ help -s cd
cd: cd [-L|-P] [dir]
Hope this helps to understand about built-in commands in Linux.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018