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=$'nt'
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.
Source link
lol