Bash parameter expansion refers to the ability to manipulate Bash shell variables in powerful ways without external utilities. Through parameter expansion modifiers, Bash scripters can achieve substitution, search operations, substring retrieval, and more using a compact, intuitive syntax. This makes parameter expansion invaluable for meeting many string processing needs in Bash scripts.
This comprehensive guide explores practical parameter expansion use cases for Linux power users and coders.
An Introduction to Bash Parameter Expansion
Shell variables in Bash hold data values that can be strings, integers, and more. The $var syntax accesses the content stored in a variable var.
Parameter expansion augments this concept by providing modifiers for $var to transform its value or substitute parts matching patterns without needing external tools like sed or awk.
For example:
var="Hello World"
echo ${var/World/Universe} # Substitute "World" with "Universe"
The above prints "Hello Universe" by replacing "World" in $var with "Universe" at the first match.
Parameter expansions manipulate variables containing parameter data like strings. But the concept applies more broadly to expanding/transforming any parameter-like entity, including positional arguments.
The main categories of parameter expansion are:
- Conditional – Check if variable set or unset
- Substring – Extract partial string data
- Search/replace – Find and replace matching patterns
Let‘s explore each in detail through applied examples.
Conditional Parameter Expansions
These parameter expansion expressions test whether a variable exists or contains data.
Default Value Assignment: -
The - expansion assigns a default string if the variable lacks a value.
For example:
echo ${testVar:-"Default If Unset"}
This prints the default string if $testVar is unset.
Value Assignment If Unset: :=
The := variant sets the variable to the given string if currently unset:
echo ${testVar:="Initial Value"}
echo $testVar # Contains "Initial Value"
The := expansion both tests and conditionally sets the variable.
Alternative Expansion If Set: :+
You can also substitute an alternative string if the variable already contains data:
testVar="Content"
echo ${testVar:+"Alternative Data"} # Prints "Alternative Data"
The above replaces $testVar with a different string since it was previously assigned.
Substring Expansions
You can extract partial substring data from variables with this syntax:
${var:start:length}
For example:
data="LinuxHint"
echo ${data:0:4} # Prints "Linux"
str="Programming Guide"
echo ${str:11:5} # Extracts "Guide"
Omitting length extracts the remainder:
str="Sample Data"
echo ${str:7} # Prints "Data"
Using Operators for String Manipulation
Additional substring capabilities come through operators like:
#– Remove matching prefix pattern%– Remove matching suffix pattern/– Find and replace first match of pattern//– Replace all matches globally
And more.
Prefix/Suffix Removal
To strip a matching prefix or suffix from a string:
var="FileIndex"
echo ${var#File} # Prints "Index"
echo ${var%dex} # Prints "FileIn"
This removes the shortest matching text from the start/end. Use ## and %% for greedy longest string removal.
Search and Replace
Implement search/replace operations by using:
${var/find/replace} - First match
${var//find/replace} - Global replace
For example:
text="Learn Bash Shell"
echo ${text/Shell/Scripting} # Replaces first "Shell"
echo ${text//Shell/Scripting} # Replaces both
This substitutes "Scripting" for "Shell" at all occurrences with //.
Leveraging Parameter Expansion for Scripting
Parameter expansion aids complex Bash scripting tasks involving:
- Text manipulation
- String sanitization
- Input validation
- Dynamic data assignment
- Configurable defaults
- Conditional variable setting
- Prefix/suffix stripping
- Multi-step pipelines
And more.
Here are some examples of parameter expansion for scripting use cases.
User Input Validation
To perform input validation:
read -p "Enter username:" name
username=${name:?Error: Unspecified username}
The : expansion checks if $name is unset, throwing an error message if so.
We can also ensure a non-empty input:
username=${name:?"${name}" cannot be empty}
This is useful for enforcing mandatory user inputs.
Dynamic Configuration
Say we have a configuration file /etc/app/config with this structure:
TYPE=service
NAME=AppData
Extract the capitalized name via parameter expansion like so:
source /etc/app/config
configName=${NAME^} # Capitalize string
Now $configName contains "Appdata". This shows data extraction combined with text transformation.
Prefix/Suffix Sanitization
To sanitize user-provided strings by removing bad prefixes/suffixes:
filename=${1#"${1%%[!0-9A-Za-z._-]}"}} # Front remove
filename=${filename%%"${filename##*[!0-9A-Za-z._-]}"}} # Back remove
This scrubs troublesome special characters from start and end.
Pipeline Usage
Parameter expansions can feed into pipelines for additional processing:
filenames="/tmp/*"
count=$(( $(echo ${filenames// /+} | wc -w) ))
Here string replacement generates a parsed word count.
Iteration
To iterate through words in a string with parameter expansions:
for word in ${string[@]}; do
echo "Word: $word"
done
This splits $string on spaces, allowing iteration on each word.
The examples showcase the scripting versatility of parameter expansion for transforms, validation, sanitization, and more.
Common Parameter Expansion Pitfalls
While parameter expansion is powerful, watch out for some subtleties:
Unbalanced quotes
echo ${var:0:1"} # Syntax error due to unbalanced quotes
Greediness
The # and % operators perform greedy longest match removal by default unlike search/replace.
Case sensitivity
Patterns are case sensitive by default unlike substitutions:
echo ${text/#code/Data} # No replacement here
Empty replacement
Take care replacing with an empty string since it deletes content:
original="Sample123"
echo ${original//[0-9]/} # Removes numbers, prints "Sample"
Boundary whitespace
The search/replace operators strip trailing newlines during substitution unlike string assignment:
echo ${var/old/new} # Strips newline
var="new" # Keeps newline
Keep these nuances in mind when leveraging parameter expansions.
Alternatives to Parameter Expansion
While parameter expansion meets many string processing needs, other tools like awk, sed, grep have their place:
awk – Advanced text processing language suited for structure data manipulation
sed – Stream editor for filtering and transforming text
grep – Specialized matching with regular expressions
Parameter expansion complements their capabilities for lightweight inline string modifications in Bash scripts.
Here is a comparison table highlighting the strengths of each approach:
| Tool | Strengths |
|---|---|
| Parameter Expansion | Simple string manipulation inline in Bash |
| awk | Advanced multi-step text processing |
| sed | Robust stream transformation |
| grep | Powerful regex matching |
The choice depends on the specific context – parameter expansion fits cleanly into shell scripts while awk/sed lend themselves for standalone invocation.
Conclusion
Bash parameter expansion provides a toolkit for manipulating variables and strings without external utilities. Through compact yet versatile syntax modifiers, code can achieve:
- Dynamic defaults
- Substring extraction
- Find/replace operations
- Prefix/suffix removal
- Conditional checking
- String sanitization
- Input validation
- Data parsing/extraction
- Text transformations
Mastering parameter expansion unlocks seamless string processing directly in Bash with reduced subshell invocations. This accelerates scripting workflows and makes Bash a more self-sufficient language.
The extensive examples provide a foundation; explore further combinations and edge cases tailored to your specific scripting tasks. Parameter expansion cuts across so many administration and coding challenges that gaining fluency proves invaluable for productive Linux scripting.


