Table of Contents
- What is Bash?
- Getting Started with Bash
- Basic Bash Commands
- Variables and Data Types
- Control Structures
- Functions
- Input/Output Redirection
- Automating Tasks with Bash Scripts
- Best Practices
- Conclusion
- References
What is Bash?
Bash, short for Bourne Again SHell, is a Unix shell and command-language interpreter. It was developed in 1989 by Brian Fox as a free, open-source replacement for the original Bourne Shell (sh), which was created in the 1970s. Today, Bash is the default shell for Linux, macOS (until macOS Catalina, which switched to Zsh), and many Unix-like systems.
Key Features of Bash:
- Interactive Mode: Type commands directly into the terminal and see results immediately.
- Scripting Language: Write reusable scripts to automate tasks (e.g., backups, log rotation, system monitoring).
- Command History: Use the
↑/↓arrows to recall past commands (orhistoryto list them). - Tab Completion: Press
Tabto auto-complete commands, filenames, or directories. - Customization: Personalize prompts, aliases, and behavior via configuration files like
~/.bashrcor~/.bash_profile.
Getting Started with Bash
To use Bash, you’ll need a terminal emulator (e.g., GNOME Terminal, Konsole, or iTerm2 on macOS). Most Linux systems open the terminal with Ctrl+Alt+T; on macOS, use Cmd+Space and search for “Terminal.”
The Bash Prompt
When you open the terminal, you’ll see a prompt that typically looks like:
user@hostname:~$
user: Your username.hostname: The name of your machine.~: The current working directory (~is shorthand for your home directory, e.g.,/home/user).$: The prompt symbol (root users see#instead).
Basic Navigation
Before diving into automation, master these essential navigation commands:
| Command | Purpose | Example |
|---|---|---|
pwd | Print the current working directory | pwd → /home/user/documents |
ls | List files/directories in the current directory | ls → file1.txt file2.jpg |
cd [path] | Change directory | cd documents → Move to documents |
cd .. | Move up one directory | cd .. → Go from /home/user/docs to /home/user |
cd ~ or cd | Return to home directory | cd ~ → /home/user |
Pro Tip: Customize ls
Add flags to ls for more details:
ls -l: Long format (shows permissions, size, modified date).ls -a: Show hidden files (files starting with.).ls -la: Combine-land-afor detailed hidden files.
Basic Bash Commands
Bash relies on commands to interact with the system. Below are foundational commands every user should know:
File/Directory Management
| Command | Purpose | Example |
|---|---|---|
mkdir [dir] | Create a directory | mkdir projects → New projects folder |
touch [file] | Create an empty file | touch notes.txt → New text file |
cp [src] [dest] | Copy files/directories | cp notes.txt backup/ → Copy to backup |
mv [src] [dest] | Move/rename files/directories | mv oldname.txt newname.txt → Rename |
rm [file] | Delete a file (permanent!) | rm trash.txt |
rm -r [dir] | Delete a directory and its contents | rm -r old_projects |
File Content Inspection
| Command | Purpose | Example |
|---|---|---|
cat [file] | Print file contents to the terminal | cat story.txt |
head [file] | Print the first 10 lines of a file | head log.txt → First 10 lines |
tail [file] | Print the last 10 lines of a file | tail -n 5 log.txt → Last 5 lines |
grep [pattern] [file] | Search for a text pattern in a file | grep "error" log.txt → Find “error” in log |
System Information
| Command | Purpose | Example |
|---|---|---|
whoami | Print your username | whoami → user |
df -h | Show disk space usage (human-readable) | df -h → Free/used space on drives |
top | Monitor running processes (interactive) | top → Close with q |
Variables and Data Types
Bash uses variables to store data (e.g., filenames, paths, or user input). Unlike programming languages like Python, Bash is weakly typed—variables are treated as strings by default, but numbers are supported for arithmetic.
Declaring Variables
Variables are declared with VAR_NAME=value (no spaces around =!):
name="Alice"
age=30
To access a variable, prefix it with $:
echo "Hello, $name! You are $age years old."
# Output: Hello, Alice! You are 30 years old.
Environment Variables
Environment variables are system-wide variables that control Bash behavior. Common examples:
$HOME: Path to your home directory (e.g.,/home/user).$PATH: Colon-separated list of directories where Bash searches for commands (e.g.,/usr/bin:/bin).$USER: Your username.
View all environment variables with env or printenv.
Special Variables
Bash has built-in special variables for scripting:
| Variable | Purpose | Example |
|---|---|---|
$0 | Name of the current script | In script.sh, $0 → ./script.sh |
$1, $2... | Arguments passed to a script/function | ./script.sh arg1 arg2 → $1=arg1, $2=arg2 |
$# | Number of arguments passed | ./script.sh a b c → $#=3 |
$? | Exit code of the last command (0 = success, non-zero = error) | ls non_existent_file; echo $? → 2 (error) |
Reading User Input
Use the read command to capture input from the user:
echo "Enter your name:"
read username # Stores input in $username
echo "Welcome, $username!"
Control Structures
Control structures (conditionals and loops) let you add logic to Bash scripts, enabling decisions and repetition.
Conditionals: if-else
Bash uses if statements to execute code based on conditions. The syntax is:
if [ condition ]; then
# Code if condition is true
elif [ another_condition ]; then
# Code if first condition is false, second is true
else
# Code if all conditions are false
fi # Closes the if statement
Common Conditions:
-f "file.txt": True iffile.txtexists and is a regular file.-d "dir": True ifdirexists and is a directory.$a -eq $b: True ifaequalsb(numeric comparison)."$str1" = "$str2": True ifstr1equalsstr2(string comparison).
Example: Check if a file exists:
file="data.txt"
if [ -f "$file" ]; then
echo "$file exists!"
else
echo "$file not found."
fi
Loops: for and while
for Loops
Iterate over a list of items (e.g., filenames, numbers):
# Loop over numbers 1-5
for i in {1..5}; do
echo "Count: $i"
done
# Loop over files in the current directory
for file in *.txt; do
echo "Found text file: $file"
done
while Loops
Repeat code as long as a condition is true:
# Countdown from 5
count=5
while [ $count -gt 0 ]; do
echo "T-minus $count..."
count=$((count - 1)) # Arithmetic: $((...))
sleep 1 # Pause for 1 second
done
echo "Blast off!"
Functions
Functions let you reuse code. They are defined with:
function_name() {
# Code here
echo "Hello from the function!"
}
Example: A function to create a directory and navigate into it:
mkcd() {
mkdir -p "$1" # -p creates parent dirs if needed
cd "$1"
}
# Use the function:
mkcd new_project # Creates "new_project" and cd into it
Input/Output Redirection
Bash lets you redirect standard input (stdin), standard output (stdout), and standard error (stderr) to/from files or other commands.
Redirecting Output
-
>: Overwrite a file with stdout.echo "Hello" > greeting.txt # Writes "Hello" to greeting.txt (overwrites if exists) -
>>: Append stdout to a file.echo "World!" >> greeting.txt # Adds "World!" to the end of greeting.txt
Redirecting Input
<: Read stdin from a file instead of the keyboard:
sort < unsorted.txt > sorted.txt # Sort "unsorted.txt" and save to "sorted.txt"
Pipes (|)
Pipes (|) send the stdout of one command to the stdin of another. This is powerful for chaining commands:
# List all .txt files, search for "report", and count results
ls *.txt | grep "report" | wc -l
Redirecting Errors
Use 2> to redirect stderr (errors) to a file:
ls non_existent_file 2> errors.log # Sends "No such file" error to errors.log
Automating Tasks with Bash Scripts
Now that you understand the basics, let’s automate! A Bash script is a text file containing a sequence of Bash commands.
Step 1: Create a Script
Start with a shebang (#!/bin/bash) to tell the system to use Bash:
#!/bin/bash
# This is a comment (starts with #)
echo "Hello, Automation!"
Save the file as hello.sh.
Step 2: Make It Executable
Scripts need execute permission to run. Use chmod +x:
chmod +x hello.sh
Step 3: Run the Script
Execute the script with ./script.sh (the ./ ensures the current directory is searched):
./hello.sh
# Output: Hello, Automation!
Example: Backup Automation Script
Let’s write a script to back up a directory (e.g., ~/photos) to a compressed archive:
#!/bin/bash
# Define variables
SOURCE_DIR="$HOME/photos"
BACKUP_DIR="$HOME/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S) # e.g., 20240520_143022
BACKUP_FILE="$BACKUP_DIR/photos_backup_$TIMESTAMP.tar.gz"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Compress and backup the source directory
echo "Creating backup: $BACKUP_FILE"
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"
# Check if backup succeeded
if [ $? -eq 0 ]; then
echo "Backup completed successfully!"
else
echo "Backup failed!" >&2 # Redirect error message to stderr
exit 1 # Exit with error code 1
fi
Run it with ./backup.sh—you’ll get a timestamped .tar.gz file in ~/backups!
Best Practices for Bash Scripting
To write reliable, maintainable scripts:
- Use Comments: Explain complex logic with
#. - Quote Variables: Always quote variables (e.g.,
"$file") to handle spaces in filenames. - Check Exit Codes: Use
set -eat the top of scripts to exit on errors automatically:#!/bin/bash set -e # Exit if any command fails - Test Scripts: Run with
bash -n script.shto check for syntax errors before execution. - Avoid Hardcoding Paths: Use variables (e.g.,
$HOME) instead of absolute paths like/home/user.
Conclusion
Bash is more than just a terminal tool—it’s a gateway to automating repetitive tasks, managing systems, and streamlining workflows. From basic navigation to writing complex scripts, mastering Bash empowers you to work smarter, not harder, in Linux.
Start small: automate a daily task (e.g., backing up files or cleaning logs), then expand to more advanced projects (e.g., monitoring scripts or deployment pipelines). With practice, you’ll unlock the full potential of Linux automation!
References
- GNU Bash Manual
- Bash Scripting Tutorial
- Linux man pages
- ShellCheck (Tool to lint Bash scripts for errors)