https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
#!/bin/bash
#### Basic Parameter Expansion ####
# Basic parameter expansion - substitutes the value of a variable
var="Hello World"
echo ${var}
# Use braces to protect the variable name from adjacent text
echo ${var}suffix
# Indirect expansion - expands using the value of a variable as the variable name
indirect_var="var"
echo ${!indirect_var}
#### Special Parameters ####
# $0 - Name of the script itself
echo $0
# $1, $2, etc. - Positional parameters (command-line arguments)
echo "First argument: $1"
echo "Second argument: $2"
# $# - Number of positional parameters
echo "Number of arguments: $#"
# $@ - All positional parameters as separate strings (preserves quoting)
echo "All arguments (as separate strings): $@"
# $* - All positional parameters as a single string
echo "All arguments (as one string): $*"
# $? - Exit status of the most recently executed command (0 = success)
ls >/dev/null
echo "Exit status: $?"
# $$ - Process ID of the current shell
echo "Shell PID: $$"
# $! - Process ID of the most recently backgrounded command
sleep 5 &
echo "Background process PID: $!"
# $_ - Last argument of the previous command
echo "Last argument: $_"
#### Default Values and Error Handling ####
# Default value if variable is unset or null
unset empty_var
echo ${empty_var:-"Default value"}
# Assign default value if variable is unset or null
: ${empty_var:="New default"}
echo ${empty_var}
# Display error if variable is unset or null
# : ${required_var:?"This variable is required"}
# Use alternate value if variable exists and is not null
echo ${var:+"Alternate value since var exists"}
#### String Operations ####
# Substring expansion - characters from position 6 to end
echo ${var:6}
# Substring expansion - 5 characters starting at position 6
echo ${var:6:5}
# Substring from end using negative offset (must include space)
echo ${var: -5}
# Length of string
echo ${#var}
# Remove shortest match from beginning
filename="bash.bashrc.backup"
echo ${filename#*.}
# Remove longest match from beginning
echo ${filename##*.}
# Remove shortest match from end
echo ${filename%.*}
# Remove longest match from end
echo ${filename%%.*}
# Replace first occurrence of pattern
sentence="The cat sat on the mat"
echo ${sentence/cat/dog}
# Replace all occurrences of pattern
echo ${sentence//the/a}
# Replace pattern at beginning (must match start)
echo ${sentence/#The/A}
# Replace pattern at end (must match end)
echo ${sentence/%mat/carpet}
#### Case Modification ####
# Convert first character to uppercase
lowercase="hello world"
echo ${lowercase^}
# Convert all characters to uppercase
echo ${lowercase^^}
# Convert first character to lowercase
uppercase="HELLO WORLD"
echo ${uppercase,}
# Convert all characters to lowercase
echo ${uppercase,,}
# Transform to uppercase (parameter@operator)
echo ${lowercase@U}
# Quote the value for reuse as input
complex='$var with spaces and "quotes"'
echo ${complex@Q}