1. Basic Syntax and Common Operators
| Category | Syntax | Description | Example (var=”hello”) |
|---|---|---|---|
| Basic Expansion | <span>${param}</span> |
Get the value of a variable, equivalent to $param but safer. | <span>${var}</span> -> hello |
| Indirect Expansion | <span>${!param}</span> |
Get the value of a variable whose name is the value of param. | <span>foo=bar; var=foo; echo ${!var}</span> -> bar |
| Get Length | <span>${#param}</span> |
Get the string length of the variable value. | <span>${#var}</span> -> 5 |
| Default Value | <span>${param:-word}</span> |
If <span>param</span> is unset or empty, use <span>word</span>, otherwise use the value of <span>param</span>. |
<span>${var:-world}</span> -> hello; <span>${unset_var:-world}</span> -> world |
| Assign Default Value | <span>${param:=word}</span> |
If <span>param</span> is unset or empty, use <span>word</span> and assign it to <span>param</span>, otherwise use the value of <span>param</span>. |
<span>${unset_var:=world}</span> -> world (and unset_var is assigned the value world) |
| Substitute Non-empty Value | <span>${param:+word}</span> |
If <span>param</span> is set and non-empty, use <span>word</span>, otherwise use an empty value. |
<span>${var:+world}</span> -> world ; <span>${unset_var:+world}</span> -> empty |
| Error Check | <span>${param:?word}</span> |
If <span>param</span> is unset or empty, write <span>word</span> to standard error and exit. |
<span>${unset_var:?Error}</span> -> outputs “bash: unset_var: Error” and exits |
<span>:-</span>,<span>:=</span>,<span>:+</span>,<span>:?</span>only<span>:=</span>actually modifies the value of the variable, the other three only perform conditional checks and value substitutions.
2. Substring Operations
| Syntax | Description | Example (var=”abcdefgh”) |
|---|---|---|
<span>${param:offset}</span> |
Extract from <span>offset</span> to the end. |
<span>${var:3}</span> -> defgh |
<span>${param:offset:length}</span> |
Extract a substring of <span>length</span> starting from <span>offset</span>. |
<span>${var:3:2}</span> -> de; <span>${var: -3}</span> -> fgh |
Note:
<span>offset</span>is calculated starting from<span>0</span>. If<span>offset</span>is negative, it counts from the end (starting from -1), and needs to be spaced or enclosed in parentheses, such as<span>${var: -3}</span>or<span>${var:(-3)}</span>.
3. Pattern Matching (Deletion)
This type of expansion is used to delete parts of a string that match a specified pattern from the <span>beginning</span> or <span>end</span>. The pattern is a <span>wildcard</span> pattern, not a regular expression.
| Syntax | Description | Example (var=”file.txt.bak”) |
|---|---|---|
<span>${param#pattern}</span> |
Check from the beginning, deleting the shortest matching <span>pattern</span> |
<span>${var#*.}</span> -> txt.bak (deletes file.) |
<span>${param##pattern}</span> |
Check from the beginning, deleting the longest matching <span>pattern</span> |
<span>${var##*.}</span> -> bak (deletes file.txt.) |
<span>${param%pattern}</span> |
Check from the end, deleting the shortest matching <span>pattern</span> |
<span>${var%.*}</span> -> file.txt (deletes .bak) |
<span>${param%%pattern}</span> |
Check from the end, deleting the longest matching <span>pattern</span> |
<span>${var%%.*}</span> -> file (deletes .txt.bak) |
<span>Memory Tips:</span>
<span>#</span>starts from the beginning (as it usually appears at the start of comments).<span>%</span>starts from the end (the percentage sign is at the end).- One symbol
<span>#</span>,<span>%</span>indicates the shortest match (<span>non-greedy</span>). - Two symbols
<span>##</span>,<span>%%</span>indicate the longest match (<span>greedy</span>).
4. Find and Replace
| Syntax | Description | Example (var=”I love apple, apple is good”) |
|---|---|---|
<span>${param/pattern/str}</span> |
Replace the first matching <span>pattern</span> with <span>str</span> |
<span>${var/apple/orange}</span> -> I love orange, apple is good |
<span>${param//pattern/str}</span> |
Replace all matching <span>pattern</span> with <span>str</span> |
<span>${var//apple/orange}</span> -> I love orange, orange is good |
<span>${param/#pattern/str}</span> |
Only replace if <span>pattern</span> matches the beginning |
<span>${var/#I/You}</span> -> You love apple, apple is good |
<span>${param/%pattern/str}</span> |
Only replace if <span>pattern</span> matches the end |
<span>${var/%good/great}</span> -> I love apple, apple is great |
5. Case Conversion (Bash 4.0+)
| Syntax | Description | Example (var=”Hello World”) |
|---|---|---|
<span>${param^^}</span> |
Convert all characters to uppercase | <span>${var^^}</span> -> HELLO WORLD |
<span>${param,,}</span> |
Convert all characters to lowercase | <span>${var,,}</span> -> hello world |
<span>${param^}</span> |
Convert the first character to uppercase | <span>${var^}</span> -> Hello World (no change as it is already capitalized) |
<span>${param,}</span> |
Convert the first character to lowercase | <span>${var,}</span> -> hello World |
Linux Shell Expansion Mechanism: Parameter Expansion