Introduction
The $
character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
Read more in the Bash User Manual.
Syntax
- ${parameter:offset} # Substring starting at offset
- ${parameter:offset:length} # Substring of length "length" starting at offset
- ${#parameter} # Length of parameter
- ${parameter/pattern/string} # Replace the first occurrence of pattern with string
- ${parameter//pattern/string} # Replace all occurrences of pattern with string
- ${parameter/#pattern/string} # Replace pattern with string if pattern is at the beginning
- ${parameter/%pattern/string} # Replace pattern with string if pattern is at the ending
- ${parameter#pattern} # Remove shortest match of pattern from beginning of parameter
- ${parameter##pattern} # Remove longest match of pattern from beginning of parameter
- ${parameter%pattern} # Remove shortest match of pattern from end of parameter
- ${parameter%%pattern} # Remove longest match of pattern from end of parameter
- ${parameter:-word} # Expand to word if parameter unset/undefined
- ${parameter:=word} # Expand to word if parameter unset/undefined and set parameter
- ${parameter:+word} # Expand to word if parameter set/defined