// Automate everything. Own your shell.
THE SHELL IS YOUR POWERHOUSE.
While others click around their GUIs, you'll be orchestrating your entire system with a few keystrokes. Bash scripts turn repetitive tasks into single commands. Your time is too valuable to waste on manual work.
AUTOMATION IS FREEDOM.
Write a script once, run it forever. Schedule backups, monitor systems, process files, deploy applicationsβall automatically. When your computer works for you, you're finally in control.
THE UNIX PHILOSOPHY LIVES IN BASH.
Small tools, combined beautifully. Pipes that transform data like magic. This is how the original hackers built the internetβand how you can too.
Click a lesson to begin
Create and execute your first bash script. Learn the shebang and basic script structure.
BeginnerStore data in variables, work with command-line arguments, and understand environment variables.
BeginnerMake decisions with if/else statements, test conditions, and boolean logic.
BeginnerIterate with for and while loops. Process multiple files and automate repetitive tasks.
BeginnerOrganize code with functions, pass parameters, and return values.
BeginnerWork with arrays, manipulate strings, and use advanced text processing.
IntermediateCheck command success, handle errors gracefully, and debug your scripts.
IntermediateBuild practical scripts for backups, monitoring, and system administration.
IntermediateMaster stdin, stdout, stderr, pipes, and here documents.
IntermediateUse sed, awk, and grep for powerful text manipulation.
IntermediateParse command-line options with getopts and manage arguments.
IntermediateDebug scripts, optimize performance, and write production-ready code.
AdvancedEvery bash script starts with a shebangβthe magic line that tells the system which interpreter to use:
Let's create a simple script that greets you:
Now make it executable and run it:
Lines starting with # are comments. Use them liberallyβyour future self will thank you:
#!/bin/bash must be the very first line of your script. No blank lines before it!
1. What does the shebang line do?
Hint: It tells Linux how to run the script.
2. What command makes a script executable?
Hint: change mode + execute
Variables store data for later use. No dollar sign when assigning, but use $ when reading:
name="Rebel" works, but name = "Rebel" does not!
Capture command output into variables using $(command) or backticks:
Scripts can accept arguments passed from the command line:
Example script:
Access system information through environment variables:
1. How do you read a variable value in bash?
Hint: It starts with a dollar sign.
2. What variable holds all command-line arguments?
Make decisions based on conditions:
Numbers use these operators:
Strings use these operators:
Check file properties:
if [ -f "$filename" ]; then
rm "$filename"
echo "Deleted $filename"
else
echo "File not found"
fi
1. What operator checks if two strings are equal?
2. What flag checks if a file is a directory?
Iterate over a list of items:
Output:
Process all files in a directory:
Use brace expansion for number ranges:
Loop while a condition is true:
Read file line by line:
for file in *.log; do
if grep -q "error" "$file"; then
echo "Found error in: $file"
break
fi
done
1. What command skips to the next iteration of a loop?
2. What syntax loops from 1 to 10?
Two ways to define functions in bash:
Functions use $1, $2, etc. just like scripts:
Bash functions return exit codes (0 = success, non-zero = error):
Use local to keep variables from leaking:
local for variables inside functions unless you intentionally want them to be global.
1. How do you keep a variable local inside a function?
2. What does $? contain after a function returns?
Store multiple values in a single variable:
filename="report.2024.pdf"
ext="${filename##*.}" # pdf
base="${filename%.*}" # report.2024
1. How do you get the length of an array?
2. What removes the shortest match from the end of a string?
Every command returns an exit code: 0 = success, non-zero = failure:
Automatically handle errors in scripts:
Add at the top of your script:
Return specific codes for different errors:
Execute cleanup code when script exits:
1. What exit code means success?
2. What option makes the script exit on any command failure?
Make scripts run automatically:
1. What does "0 2 * * *" mean in crontab?
Hint: minute hour day month weekday
2. What does set -euo pipefail do?
Hint: It makes the script stop on errors and undefined variables.Every process has three standard streams:
Connect commands together - output of one becomes input of another:
1. What file descriptor is stderr?
2. What operator appends output to a file?
Search for patterns in text:
Find and replace text:
Powerful column-based processing:
1. Which flag makes grep case-insensitive?
2. What sed command replaces all occurrences?
Process arguments one by one:
Handle command-line flags properly:
Usage:
1. What command moves to the next argument?
2. In getopts, what stores the option letter?
Use -x to trace execution:
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
log() { echo "[$(date)] $*"; }
error() { echo "[ERROR] $*" >&2; exit 1; }
main() {
log "Starting"
# your code here
log "Done"
}
main "$@"
1. What flag enables command tracing?
2. Why quote variables?