dev-resources.site
for different kinds of informations.
The Complete Guide to Bash Commands
Published at
1/9/2025
Categories
bash
linux
automation
shell
Author
nolunchbreaks_22
Author
16 person written this
nolunchbreaks_22
open
Basic Syntax and Variables
Variables
name="John"
echo $name # Basic output
echo "$name" # Recommended way (with quotes)
echo "${name}" # Most explicit way
String Quotes
- Double quotes allow variable expansion:
"Hello $name"
- Single quotes preserve literal text:
'Hello $name'
Shell Execution
echo "Current directory: $(pwd)" # Modern syntax
echo "Current directory: `pwd`" # Legacy syntax
Control Flow
Conditional Execution
git commit && git push # Run second command only if first succeeds
git commit || echo "Commit failed" # Run second command only if first fails
Functions
get_name() {
echo "John"
}
echo "You are $(get_name)"
Conditionals
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi
Parameter Expansions
Basic Operations
name="John"
echo "${name}" # Standard output
echo "${name/J/j}" # Substitution (john)
echo "${name:0:2}" # Slicing (Jo)
echo "${name::2}" # Slicing from start (Jo)
echo "${name::-1}" # Slice except last char (Joh)
echo "${name:(-1)}" # Last character (n)
echo "${food:-Cake}" # Default value if unset
Advanced Expansions
str="/path/to/foo.cpp"
echo "${str%.cpp}" # Remove .cpp
echo "${str%.cpp}.o" # Replace with .o
echo "${str%/*}" # Remove last segment
echo "${str##*.}" # Get extension
echo "${str##*/}" # Get filename
echo "${str#*/}" # Remove first segment
echo "${str/foo/bar}" # Replace foo with bar
Arrays
Array Operations
# Definition
Fruits=('Apple' 'Banana' 'Orange')
# Access
echo "${Fruits[0]}" # First element
echo "${Fruits[-1]}" # Last element
echo "${Fruits[@]}" # All elements
echo "${#Fruits[@]}" # Number of elements
echo "${#Fruits}" # Length of first element
# Modification
Fruits+=('Watermelon') # Add element
unset Fruits[2] # Remove element
Fruits=("${Fruits[@]}") # Recreate array
Dictionaries (Associative Arrays)
# Declaration
declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"
# Access
echo "${sounds[dog]}" # Single value
echo "${sounds[@]}" # All values
echo "${!sounds[@]}" # All keys
echo "${#sounds[@]}" # Number of elements
File Conditions
[[ -e FILE ]] # Exists
[[ -r FILE ]] # Readable
[[ -h FILE ]] # Symlink
[[ -d FILE ]] # Directory
[[ -w FILE ]] # Writable
[[ -s FILE ]] # Size > 0 bytes
[[ -f FILE ]] # Regular file
[[ -x FILE ]] # Executable
String Conditions
[[ -z STRING ]] # Empty string
[[ -n STRING ]] # Not empty string
[[ STRING == STRING ]] # Equal
[[ STRING != STRING ]] # Not equal
[[ STRING =~ REGEX ]] # Regex match
Numeric Conditions
[[ NUM -eq NUM ]] # Equal
[[ NUM -ne NUM ]] # Not equal
[[ NUM -lt NUM ]] # Less than
[[ NUM -le NUM ]] # Less than or equal
[[ NUM -gt NUM ]] # Greater than
[[ NUM -ge NUM ]] # Greater than or equal
Loops
For Loops
# Basic for loop
for i in /etc/rc.*; do
echo "$i"
done
# C-style for loop
for ((i = 0; i < 100; i++)); do
echo "$i"
done
# Range loop
for i in {1..5}; do
echo "Welcome $i"
done
# With step size
for i in {5..50..5}; do
echo "Welcome $i"
done
While Loops
# Reading lines
while read -r line; do
echo "$line"
done <file.txt
# Infinite loop
while true; do
echo "Forever"
done
Input/Output
Redirection
command > output.txt # stdout to file
command >> output.txt # stdout to file (append)
command 2> error.log # stderr to file
command 2>&1 # stderr to stdout
command &>/dev/null # stdout and stderr to null
Reading Input
echo -n "Proceed? [y/n]: "
read -r ans
echo "$ans"
read -n 1 ans # Read single character
Special Variables
$? # Exit status of last command
$! # PID of last background process
$$ # Current shell PID
$0 # Script name
$_ # Last argument of previous command
$# # Number of arguments
$@ # All arguments as separate words
$* # All arguments as a single word
Debugging and Error Handling
Strict Mode
set -euo pipefail
IFS=$'\n\t'
Trap Errors
trap 'echo Error at about $LINENO' ERR
# Or with function
traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}
set -o errtrace
trap traperr ERR
Advanced Features
Case Statements
case "$1" in
start|up)
vagrant up
;;
stop|down)
vagrant halt
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
Here Documents
cat <<END
hello world
END
Command Substitution
result=$(command)
lines=(`cat "logfile"`)
Best Practices
- Always quote variables unless you specifically need word splitting
- Use
set -e
to make scripts exit on error - Use meaningful variable names
- Comment your code
- Use functions for repeated operations
- Check for required commands at script start
- Provide usage information for scripts
- Use shellcheck to validate your scripts
Remember that Bash is both powerful and complex. These commands and constructs can be combined in numerous ways to create sophisticated scripts. Always test your scripts thoroughly, especially when dealing with file operations or system commands.
bash Article's
30 articles in total
Understanding Linux Shells: Interactive, Non-Interactive, and RC Files
read article
Fixing Linux Backup Sync Issues for exFAT Compatibility
read article
Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv
read article
Changing the Login Shell with Security and Interactivity
read article
Final Bash Script Series Mastering Remote Server Management and Web App Deployment
read article
Writting a GH extension with AI
read article
Test GPIO pins on BeagleBone Black by toggling high to low
read article
NYP Infosec December CTF 2024 writeup!
read article
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3
read article
The Complete Guide to Bash Commands
currently reading
How to Get Started with Bash Scripting for Automation
read article
Building a Basic Testing Framework in Bash 🐚
read article
Funny how, as I was writing this, I started wondering—could we make this pipeline even smarter? That’s when SonarQube came to mind. But can it even work with Bash scripts? I’d love to hear your thoughts!
read article
Domina Bash con ejemplos prácticos de Git
read article
Explaining Null Device or Black Hole Register in Vim
read article
A Media Server on Steroids - Walkthrough
read article
From FZF file preview to a browser for cht.sh to discovering the ideal solution
read article
Code. Battery notifier.
read article
🌟Simplifying User Account Management with Bash Scripting Day3🌟
read article
Become a Bash Scripting Pro in 10 Minutes: A Quick Guide for Beginners
read article
Automatically Monitor and Manage RAM Usage for Next.js Development
read article
Bash Script - Task Management
read article
Bash Your Tools
read article
Bash Script Series: Automating Log Analysis with Bash Script or Shell Script
read article
The Most Interesting Mostly Tech Reads of 2024
read article
🚀 Automating Process Monitoring & Restarting with Bash Scripting Day4! 🔧
read article
🚀 RazzShell v1.0.1 is Here: Plugin Support, Enhanced Job Management, and More! 🌟
read article
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
read article
Simplify GitHub Workflow Management with My-GitHub-Manager
read article
How to play a bunch of lemmings
read article
Featured ones: