Bash Basics¶
1. What is a Bash script?¶
A Bash script is a text file with terminal command executed in order.
Example:
The first line is called the shebang:
It tell Linux to run the file using Bash.
To execute the script:
2. Basic script structure¶
A clean Bash script can look like this:
Explanation:
| Option | Meaning |
|---|---|
-e |
Exit immediately if a command fails |
-u |
Treat undefined variables as errors |
-o pipefail |
A pipeline fails if any command inside it fails |
3. Variables¶
Declare variables without spaces around =:
Important:
Warning
Use quotes when expanding variables. This avoids problems with spaces or empty values.
4. Script arguments¶
Arguments are values passed when launching the script:
Example:
Inside the script:
#!/usr/bin/env bash
set -euo pipefail
RUN_DIR="$1"
COV_DB="$2"
printf 'Run directory: %s\n' "$RUN_DIR"
printf 'Coverage DB: %s\n' "$COV_DB"
Special argument variables:
| Variable | Meaning |
|---|---|
$0 |
Script name |
$1, $2, ... |
First, second, etc. argument |
$# |
Number of arguments |
$@ |
All arguments, safely separated |
5. Required arguments with useful errors¶
This pattern is very useful:
Meaning:
- Use argument
$1 - If
$1is missing or empty, stop the script - Print the error
missing RUN_DIR
Example:
#!/usr/bin/env bash
set -euo pipefail
RUN_DIR="${1:?missing RUN_DIR}"
printf 'RUN_DIR = %s\n' "$RUN_DIR"
If you run:
Bash will stop with an error.
6. Default values¶
Use this syntax to provide a default value:
Meaning:
- If
MODEis defined and not empty, use it - Otherwise, use
debug
Example:
Run normally:
Output:
Run with a custom value:
Output:
7. Checking files and directories¶
Bash has useful test operators.
Common checks:
| Check | Meaning |
|---|---|
-d "$path" |
Path exists and is a directory |
-f "$path" |
Path exists and is a regular file |
-e "$path" |
Path exists |
-z "$var" |
String is empty |
-n "$var" |
String is not empty |
Negation uses !:
Meaning:
means:
If
RUN_DIRis not a directory
8. Functions¶
Functions help organize your script.
Use local for variables inside functions:
check_dir() {
local dir="${1:?missing dir}"
if [[ ! -d "$dir" ]]; then
printf '[FAIL] Directory does not exist: %s\n' "$dir"
return 1
fi
}
Then call it:
9. Reading command output into a variable¶
The $() syntax in Bash is used for command substitution. It allows you to execute a
command and substitute its output in place of the $() expression. This is useful
when you want to use the output of a command as an argument for another command
or assign it to a variable.
Example:
10. Reading values from a file with source¶
Suppose you have a file called coverage.mk
You can load it is Bash with:
After that, the variables are available:
Example:
#!/usr/bin/env bash
set -euo pipefail
RUN_MANIFEST_FILE="${1:?missing RUN_MANIFEST_FILE}"
if [[ ! -f "$RUN_MANIFEST_FILE" ]]; then
printf '[FAIL] Manifest file does not exist: %s\n' "$RUN_MANIFEST_FILE"
exit 1
fi
source "$RUN_MANIFEST_FILE"
if [[ -z "${RUN_COV_DB:-}" ]]; then
printf '[FAIL] RUN_COV_DB is empty\n'
exit 1
fi
printf 'RUN_COV_DB = %s\n' "$RUN_COV_DB"
Reference Material¶
Websites
Books