Absolutely! Shell builtins are commands that are integrated into the shell itself (like Bash) rather than external programs. Using builtins is usually faster, and some are required for scripts to work properly. Let me give you a complete guide with examples.
- Executed directly by the shell, no separate binary required.
- Essential for control flow, variables, and environment management.
- Can be listed with:
helpor
compgen -b| Builtin | Description | Example |
|---|---|---|
export |
Mark variable for child processes | export PATH=$PATH:/my/bin |
readonly |
Make a variable read-only | readonly PI=3.14 |
unset |
Remove a variable | unset VAR |
declare / typeset |
Declare variables with attributes | declare -i NUM=5 |
local |
Declare function-local variable | local VAR="value" |
let |
Arithmetic operations | let COUNT=COUNT+1 |
eval |
Evaluate a string as command | eval "$CMD" |
printf |
Formatted output | printf "%s\n" "$VAR" |
read |
Read input from user | read NAME |
type |
Identify command type | type cd |
readonly |
Protect variable from modification | readonly VAR |
| Builtin | Description | Example |
|---|---|---|
if / then / else / elif |
Conditional execution | if [[ $x -gt 5 ]]; then echo "Yes"; fi |
for / while / until |
Loops | for i in {1..5}; do echo $i; done |
break |
Exit loop | break |
continue |
Skip current loop iteration | continue |
return |
Return from function | return 5 |
exit |
Exit script | exit 1 |
trap |
Execute command on signals | trap 'echo signal received' SIGINT |
wait |
Wait for background process | wait $PID |
eval |
Evaluate a command string | eval "$CMD" |
| Builtin | Description | Example |
|---|---|---|
cd |
Change directory | cd /tmp |
pwd |
Print working directory | pwd |
alias |
Create shortcut | alias ll='ls -l' |
unalias |
Remove alias | unalias ll |
type |
Show how a command will be interpreted | type ls |
hash |
Remember command locations | hash -r |
command |
Execute command bypassing shell functions/aliases | command ls |
source / . |
Execute commands in current shell | source ~/.bashrc |
| Builtin | Description | Example |
|---|---|---|
set |
Set shell options | set -x (debug) |
shopt |
Set shell options (Bash-specific) | shopt -s extglob |
jobs |
List background jobs | jobs |
fg / bg |
Bring job foreground/background | fg %1 |
wait |
Wait for job | wait %1 |
trap |
Catch signals | trap "echo Ctrl+C pressed" SIGINT |
| Builtin | Description | Example |
|---|---|---|
echo |
Print text | echo "Hello" |
test / [ / [[ |
Conditional test | [[ $x -gt 5 ]] |
true |
Returns 0 | true |
false |
Returns 1 | false |
help |
Show help for builtin | help read |
wait |
Wait for background process | wait $PID |
times |
Show shell and user time | times |
type cd # cd is a shell builtin
type ls # ls is an external command#!/bin/bash
func() {
local VAR="inside"
echo "VAR: $VAR"
}
func
echo "VAR outside: $VAR" # empty#!/bin/bash
export MYVAR="hello"
bash -c 'echo $MYVAR' # prints hello#!/bin/bash
set -x
VAR=5
echo $VAR
set +x- Faster than external commands
- Required for variables, flow control, functions
- Shell-specific behavior (e.g.,
local,shopt)
✅ Summary
- Builtins are commands inside the shell.
- Categories: Variables, Flow Control, Execution, Debugging, Utilities.
- Use
helporcompgen -bto list them. - Builtins are essential for efficient and portable scripts.
If you want, I can make a Markdown cheatsheet with all Bash builtins, categorized, with examples, ready to save and reference.
Do you want me to do that?