Bash Parameter Expansion
suggest changeIntroduction
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 end
- ${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 is unset or null
- ${parameter:=word} # Expand to word if parameter is unset or null, and set parameter
- ${parameter:+word} # Expand to word if parameter is set and not null
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents