Skip to content

Latest commit

 

History

History
167 lines (123 loc) · 6.51 KB

File metadata and controls

167 lines (123 loc) · 6.51 KB

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.


📌 Shell Builtins in Bash


1. What Are Builtins?

  • Executed directly by the shell, no separate binary required.
  • Essential for control flow, variables, and environment management.
  • Can be listed with:
help

or

compgen -b

2. Categories of Builtins

a) Variable and Environment Management

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

b) Shell Flow Control

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"

c) Shell Execution / Command Handling

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

d) Debugging and Shell Options

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

e) Other Useful Builtins

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

3. Check if a command is a builtin

type cd      # cd is a shell builtin
type ls      # ls is an external command

4. Examples of Builtins in Action

a) Local variable inside function

#!/bin/bash
func() {
  local VAR="inside"
  echo "VAR: $VAR"
}
func
echo "VAR outside: $VAR"   # empty

b) Export variable

#!/bin/bash
export MYVAR="hello"
bash -c 'echo $MYVAR'   # prints hello

c) Debug with set -x

#!/bin/bash
set -x
VAR=5
echo $VAR
set +x

5. Why Use Builtins?

  • 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 help or compgen -b to 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?