Bash:ParameterSubstitution
lastmod 2025-12-30created 2015-12-0217 edits
Notes on parameter expansion (Parameter Expansion).
List
| Syntax | Description | EXAMPLE |
| | Same as $variable, but some things only work with {} | |
| | Extracts the string after the start position | |
| | Extracts a string of the given length after the start position | |
| | Sets a default value when the variable is undeclared or NULL; positional parameters cannot be used | |
| | Sets a default value only when the variable is undeclared; positional parameters cannot be used | |
| | Sets a default value when the variable is undeclared or NULL; positional parameters can be used | |
| | Sets a default value only when the variable is undeclared; positional parameters can be used | |
| | Prints the word and exits the script when the variable is undeclared or NULL, | |
| | Prints the word and exits the script only when the variable is undeclared | |
| | Uses the word only when the variable is declared | |
| | Uses the word when the variable is declared or NULL | |
| | String length | |
| | Deletes the shortest match of the word from the start of the variable | |
| | Deletes the longest match of the word from the start of the variable | |
| | Deletes the shortest match of the word from the end of the variable | |
| | Deletes the longest match of the word from the end of the variable | |
| | Replaces the first matching word | |
| | Replaces all matching words | |
| | Replaces if the start matches | |
| | Replaces if the end matches | |
| | Extracts the names of declared variables that contain the word | |
Test code: string="abc-efg-123-abc"
Upper/lowercase conversion
Since Bash 4.0, it can be done as follows.
Lowercase:
If the syntax above does not work, use awk:
If there is no awk either, use tr:
Splitting file names
Given FILE=/home/username/Project/scot.remote/20230727/cvt_pm_23-long.mp4, using "${FILE##*/}" leaves only cvt_pm_23-long.mp4.
See also
Favorite site
- [Recommended] Shell Parameter Expansion (Bash Reference Manual)
- Parameter substitution in Shell scripts
- KLDP: Advanced Bash-Scripting Guide: A complete guide to shell scripting with Bash // Manipulating Strings
- [Recommended] Key summary of Bash for beginners (Shell Script) 1
References
-
Bash_summarizes_the_key_for_beginners.pdf ↩