Skip to main content
Commands

Shell Built-in Commands

In Linux, there are shell built-in commands which you are already using but never paid attention to. Learn more about them in this tutorial.

β€”Sagar Sharma

Warp Terminal

In Linux, there are commands and then there are shell built-ins. Linux purists refrain from calling them commands or shell built-in commands.

So, what's so special about these shell built-ins and how are they different from other Linux commands? You'll learn about them in this tutorial.

What is a shell built-in?

When you run a Linux command, it is run from a binary in a sub-shell by forking the existing process. However, a shell built-in is contained in the shell itself. It runs in the same shell without creating a new process. Thus, shell built-ins are faster. cd, pwd, exit, export, alias are some of the commonly used shell built-ins.

Want to know what other shell built-in commands are there, why, and how they are used? In this tutorial, I'm going to answer all such questions.

List of shell builtin commands

To list all the builtin bash commands, all you have to do is execute the help command in the terminal:

help
list bash builtin commands in Linux

A long list of commands. Isn't it?

Now, let's have a look at why they are used in the first place:

CommandDescription
aliasUsed to create a shortcut or an alternative name for a command.
bgMoves suspended or stopped jobs in the background.
bindIt is used to show or modify the keybindings.
breakUsed within loops to terminate the loop and continue the execution outside the loop.
builtinIf you have built-in and external commands with the same name, using builtin, you can prioritize the internal command for the execution.
callerUsed in the script to get more information about that specific code block.
caseIt allows you to create multiple conditions and their outcomes based on a specific block.
cdChange directory.
commandUsed to execute external commands by bypassing any alias or functions having the same name.`
compgenIt will generate completions for command options, files, directories, and other items based on the current context.
completeAllows you to modify the command completions for specific commands.
compoptIt allows you to modify argument completion for a specific command.
continueUsed in loops to skip the execution of the remaining iteration and move to the next iteration of the given code,
coprocCreates a co-process to establish communication between the script and the background process.
declareUsed to define variables and their properties.
dirsIt shows the list of the commands that you went through recently.
disownBy using it, you can detach currently running jobs from the shell, allowing you to continue their execution even after closing the shell.
echoUsed to print output.
enableUsed to enable or disable the built-in shell commands and functions.
evalIt treats the given arguments as a command and executes it in such it was written in as script or executed directly in the terminal.
execIt is used to replace the current process with the new one.
exitUsed to terminate the current shell session or the script.
exportIt allows you to create or modify the environment variables.
falseUsed to return a non-zero exit status that indicates a false condition or failure.
fcBy using it, you can list, modify and re-execute previously used commands.
fgUsed to bring suspended, or the background processes, to the foreground.
for [condition]; do[execution];A for loop is used in the shell script to execute repetitive tasks.
function name { COMMANDS ; }Used to create a function that can be used multiple times in a shell session or the shell script.
getoptsAllows you to parse the command-line options and arguments inside a script.
hashIt helps the shell to remember the location of the executables.
helpGives information about built-in commands in the shell.
historyProvides a list of previously executed commands.
if COMMANDS; then COMMANDS;A condition statement that executed the command if the condition is true.
jobsDisplays the status of the background jobs running in the background.
killSends a signal to kill to terminate the process.
let arg [arg ...]Using the let command, you can perform the arithmetic operations.
localIt allows the user to define local variables within the specific function.
logoutAs the name suggests, it logs you out of the current user session.
mapfileReads text from an input stream or a file and stores it in an array.
popdIt is used to replace the top directory from the directory stack with the current directory.
printfUsed to format and print the variable values or the text.
pushdAllows you to change the directory and stores the previous directory to the directory stack, by which you can easily navigate between multiple directories with the least effort.
pwdWhen executed, it shows the current working directory.
readReads input from a file or user and assigns it to a variable.
readarrayUsed to read lines from the input file and turns it into an array variable.
readonlyUsed to declare and define read-only variables.
returnIt is used to exit a function and, optionally, you can also specify a return value.
selectBy using it, you can create a simple menu or interactive selection prompt for a user.
setAllows you to modify the shell settings and parameters.
shiftBy using it, you can drop the first argument and lower the remaining ones a step lower in the list of positional parameters.
shoptProvides various options to modify the behavior of the shell.
sourceIt is used to reload config files and execute the script in the current shell.
suspendUsed to suspend the current shell and returns the control to the parent shell.
testUsed within the shell script, allowing you to check conditions and perform various tests.
timeUsed to measure the command or script execution time.
timesIt is used to measure the CPU usage of a shell session or a shell script.
trapIt allows you to execute specific operations when received defined signals.
trueReturns a successful exit status.
typeInforms you about the type of the command.
typesetAllows you to define and control the behavior of the variables.
ulimitBy using it, you can manage resources for shell sessions.
umaskAllows you to control permissions given to newly created files and directories.
unaliasUsed to unset alias which was configured using the alias command.
unsetIt is used to unset variables and functions which were defined previously.
until COMMANDS; do COMMANDS; doneIt is a loop that will be iterated until the given conditions are met.
variablesProvides names and descriptions of some shell variables.
waitUsed to pause the execution until all the background tasks are complete.
while COMMANDS; do COMMANDS; doneIt is a loop that will be iterated till the specified conditions are true.
🀚
Some Linux purists frown upon calling them shell built-in commands. They prefer not addressing them as commands. However, most Linux books call them built-in shell commands so I see no harm in using the 'command' word with shell built-ins.

No man pages and binaries for shell built-in commands

Regular or should I say external Linux commands come with a man page entry that gives information on how to use them.

However, shell built-in commands don't have man pages. If you try using the man command, it will show an error like this:

Sagar@LHB:~$ man cd No manual entry for cd

You'll have to use the help command that you saw above:

help shell_built_in_command_name

And it will give you some hints on using the built-in command.

Similarly, you won't see binaries for the shell built-ins with whereis and which commands.

Identify the shell built-in command

To identify which command is built-in, you can use the type command as shown:

type -t Command

For example, if I want to check whether the cd is a built-in command or not, I will be using the following:

type -t cd
identify the built-in command using the type command in Linux
πŸ’‘
Sometimes you'll see a re-implementation of a shell built-in as an external command. echo is a common example. To identify, use type -a to see if there is any re-implementation of a built-in command.

But what if the command is not built-in? So what output can you expect from the type command?

Well, apart from the above output, it can give you four other types of output:

  • alias: If the command is an alias.
  • file: If the command is an executable file and is also configured with the PATH variable.
  • function: If the command is function.
  • No output: If the command is not of the above, then, it won't show you anything.
Use Type Command in Linux to Get More Info About Commands
The type command tells you whether a Linux command is built-in shell command, where is its executable located and whether it is aliased to some other command. Here’s how to use the type command in Linux.

More bash specials

Bash also has some special variables that you may want to know about next.

Special Variables in Bash Shell [With Script Examples]
The bash shell has some special variables that have specific usages and purposes. Learn more about them here.

Since we are discussing 'specials', how about special file permissions?

What is SUID, GUID and Sticky Bit in Linux? How to Use Them?
You see an s instead of x in the file permissions? Linux has some special file permissions called SUID, GUID and Sticky Bit. Know more about them.

I hope you liked this article on shell built-ins. Let me know if you have questions or suggestions.

Sagar Sharma
Sagar Sharma
close