LinuxCommandLibrary

Shell Scripting

Script Basics

A shell script is a text file containing commands that the shell executes in sequence. The first line should be a shebang that tells the system which interpreter to use.
$ #!/bin/bash
copy
$ #!/usr/bin/env bash
copy
Make the script executable and run it.
$ chmod +x script.sh
copy
$ ./script.sh
copy
Use `set` options at the top of scripts to catch errors early.
$ set -euo pipefail
copy
-e exits on error, -u treats unset variables as errors, -o pipefail catches failures in pipes.

Variables

Variable assignment has no spaces around the `=` sign. Use double quotes around variable references to prevent word splitting.
ExpressionDescription
VAR=valueAssign a value
$VARRead the value
${VAR}Read with explicit boundary
"$VAR"Read safely with quoting
${VAR:-default}Use default if VAR is unset or empty
${VAR:=default}Assign default if VAR is unset or empty
${VAR:+alternate}Use alternate if VAR is set and not empty
${VAR:?error msg}Exit with error if VAR is unset or empty
${#VAR}Length of the value
readonly VARMake variable read-only
export VARMake variable available to child processes

Special Variables

These are set automatically by the shell and are read-only.
VariableDescription
$0Name of the script
$1..$9Positional parameters (arguments)
${10}Positional parameters beyond 9
$#Number of arguments
$@All arguments as separate words
$*All arguments as a single string
$?Exit status of the last command
$$PID of the current shell
$!PID of the last background command
$_Last argument of the previous command

String Operations

Bash provides built-in string manipulation without needing external commands.
ExpressionDescription
${VAR#pattern}Remove shortest match from start
${VAR##pattern}Remove longest match from start
${VAR%pattern}Remove shortest match from end
${VAR%%pattern}Remove longest match from end
${VAR/old/new}Replace first occurrence
${VAR//old/new}Replace all occurrences
${VAR:offset}Substring from offset
${VAR:offset:length}Substring from offset with length
${VAR^}Uppercase first character
${VAR^^}Uppercase all characters
${VAR,}Lowercase first character
${VAR,,}Lowercase all characters

Conditionals

Use `[[ ]]` for conditionals in bash scripts. It supports pattern matching and is safer than the older `[ ]` form. Always put spaces inside the brackets.
$ if [[ -f "file.txt" ]]; then echo "exists"; fi
copy
OperatorDescription
-f fileTrue if file exists and is a regular file
-d fileTrue if file exists and is a directory
-e fileTrue if file exists (any type)
-r fileTrue if file is readable
-w fileTrue if file is writable
-x fileTrue if file is executable
-s fileTrue if file is not empty
-z stringTrue if string is empty
-n stringTrue if string is not empty
==String equality
!=String inequality
=~Regex match (inside [[ ]])
-eqNumeric equality
-neNumeric inequality
-ltNumeric less than
-leNumeric less than or equal
-gtNumeric greater than
-geNumeric greater than or equal
$ if [[ $count -gt 0 ]]; then echo "positive"; elif [[ $count -eq 0 ]]; then echo "zero"; else echo "negative"; fi
copy
The case statement matches a value against patterns.
$ case "$1" in start) echo "Starting";; stop) echo "Stopping";; *) echo "Usage: $0 {start|stop}";; esac
copy

Loops

The for loop iterates over a list of items.
$ for file in *.txt; do echo "$file"; done
copy
$ for i in {1..10}; do echo "$i"; done
copy
$ for ((i=0; i<10; i++)); do echo "$i"; done
copy
The while loop runs as long as the condition is true.
$ while read -r line; do echo "$line"; done < file.txt
copy
The until loop runs until the condition becomes true.
$ until [[ -f "ready.flag" ]]; do sleep 1; done
copy
Use break to exit a loop early and continue to skip to the next iteration.

Functions

Functions group reusable commands. Arguments are accessed with $1, $2, etc. inside the function body. Use local to keep variables scoped to the function.
$ greet() { local name="$1"; echo "Hello, $name"; }
copy
$ greet "World"
copy
A function returns its last command's exit status, or use return to set an explicit exit code (0-255). To return strings, use command substitution.
$ get_date() { date +%Y-%m-%d; }
copy
$ today=$(get_date)
copy

Arrays

Bash supports indexed arrays. Declare and manipulate them as follows.
ExpressionDescription
arr=(a b c)Declare an array
arr[0]=valueSet element by index
${arr[0]}Access element by index
${arr[@]}All elements
${#arr[@]}Number of elements
${arr[@]:1:2}Slice: 2 elements starting at index 1
arr+=(d e)Append elements
${!arr[@]}All indices
unset arr[1]Remove an element
Associative arrays require explicit declaration.
$ declare -A map
copy
$ map[key]="value"
copy
$ echo "${map[key]}"
copy

Arithmetic

Use `$(( ))` for arithmetic expressions. All standard operators are available.
ExpressionDescription
$((a + b))Addition
$((a - b))Subtraction
**$((a * b))**Multiplication
$((a / b))Integer division
$((a % b))Modulo
$((a b))**Exponentiation
$((a++))Post-increment
$((++a))Pre-increment
let "a += 5"Arithmetic assignment

Input and Output

Use read to get input from the user or from a file.
$ read -p "Enter name: " name
copy
$ read -s -p "Password: " pass
copy
$ read -r -a items <<< "a b c"
copy
Use printf for formatted output — it is more portable than echo.
$ printf "Name: %s, Age: %d\n" "$name" "$age"
copy
A here document passes multi-line text to a command.
$ cat <<EOF
copy
Lines of text go here.
Variables like $HOME are expanded.
$ EOF
copy
Use `<<'EOF'` (quoted) to prevent variable expansion inside the here document.

Exit Codes and Traps

Every command returns an exit code: 0 means success, anything else means failure. Use exit to set the script's return code.
$ exit 0
copy
$ exit 1
copy
Use trap to run cleanup code when the script exits or receives a signal.
$ trap 'rm -f /tmp/mylock' EXIT
copy
$ trap 'echo "Interrupted"' INT TERM
copy
Combine `set` options for robust scripts.
OptionDescription
set -eExit immediately on error
set -uError on undefined variables
set -o pipefailPipe fails if any command fails
set -xPrint each command before executing (debug)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard