Parameter Expansion with ${} in Bash: A Comprehensive Guide – TecAdmin
Abstract: }"12345text="The Rain in Spain"echo "First character uppercasetext="The Rain in Spain" echo "First character uppercase
In the world of Unix and Linux, Bash is a powerful and versatile shell that provides advanced scripting capabilities. One of the core features of Bash is parameter expansion, which is a method for transforming and manipulating the content of variables using the ${} syntax. In this article, we will dive into the various types of parameter expansions available in Bash and provide examples to illustrate their usage.
1. Basic Parameter ExpansionThe most basic form of parameter expansion is to simply use {variable} to reference the value of a variable. For example:
name="John Doe" echo "Hello, ${name}!"12name="John Doe"echo "Hello, ${name}!"This script will output 「Hello, John Doe!」. The use of ${} is optional when using simple variable names, but it is good practice to include it for clarity and consistency.
2. Default Value ExpansionYou can provide a default value for a variable if it is not set or is null using the {variable:-default} syntax. For example:
greeting=${name:-"World"} echo "Hello, ${greeting}!"12greeting=${name:-"World"}echo "Hello, ${greeting}!"If the variable ‘name’ is not set or is null, the default value 「World」 will be used.
3. Assign Default ValueSimilar to the previous expansion, you can use the {variable:=default} syntax to assign a default value to a variable if it is not set or is null. For example:
: {count:=0} echo "Current count: ${count}"12: {count:=0}echo "Current count: ${count}"If the variable ‘count’ is not set or is null, it will be assigned the value 0.
4. Error if Unset or NullTo raise an error if a variable is not set or is null, use the ${variable:?error_message} syntax. For example:
echo "Hello, ${name:?Name is not set}!"1echo "Hello, ${name:?Name is not set}!"This script will print an error message and exit if the variable ‘name’ is not set or is null.
5. String Length ExpansionTo find the length of a string stored in a variable, use the ${#variable} syntax. For example:
name="John Doe" echo "Name length: ${#name}"12name="John Doe"echo "Name length: ${#name}"This script will output 「Name length: 8」.
6. Substring ExpansionSubstring expansion allows you to extract a portion of a string using the {variable:offset:length} syntax. For example:
name="John Doe" echo "First name: ${name:0:4}"12name="John Doe"echo "First name: ${name:0:4}"This script will output 「First name: John」.
7. Substring RemovalBash provides two forms of substring removal:
- Remove the shortest matching prefix pattern: {variable#pattern}
- Remove the longest matching prefix pattern: ${variable##pattern}
- Remove the shortest matching suffix pattern: ${variable%pattern}
- Remove the longest matching suffix pattern: ${variable%%pattern}
For example:
filename="document.txt" echo "Without extension: ${filename%.*}"12filename="document.txt"echo "Without extension: ${filename%.*}"This script will output 「Without extension: document「.
8. Search and ReplaceTo search for a pattern and replace it with another string, use the {variable/pattern/replacement} syntax. To replace all occurrences, use ${variable//pattern/replacement}. For example:
text="The rain in Spain falls mainly on the plain." echo "Replace 'rain' with 'sun': {text/rain/sun}" echo "Replace all 'ain': ${text//ain/ane}"123text="The rain in Spain falls mainly on the plain."echo "Replace 'rain' with 'sun': {text/rain/sun}"echo "Replace all 'ain': ${text//ain/ane}"This script will output:
Output: Replace 'rain' with 'sun': The sun in Spain falls mainly on the plain. Replace all 'ain': The rane in Spane falls manely on the plane.9. Case Modification
Bash also supports modifying the case of strings with parameter expansion:
- Convert the first character to uppercase: {variable^}
- Convert the first character to lowercase: ${variable,}
- Convert all characters to uppercase: ${variable^^}
- Convert all characters to lowercase: ${variable,,}
For example:
text="The Rain in Spain" echo "First character uppercase: {text^}" echo "First character lowercase: ${text,}" echo "All characters uppercase: ${text^^}" echo "All characters lowercase: ${text,,}"12345text="The Rain in Spain"echo "First character uppercase: {text^}"echo "First character lowercase: ${text,}"echo "All characters uppercase: ${text^^}"echo "All characters lowercase: ${text,,}"This script will output:
Output: First character uppercase: The Rain in Spain First character lowercase: the Rain in Spain All characters uppercase: THE RAIN IN SPAIN All characters lowercase: the rain in spain10. Indirect Reference
Using parameter expansion, you can reference the value of a variable indirectly with ${!variable}. For example:
name_var="name" name="John Doe" echo "Indirect reference: ${!name_var}"123name_var="name"name="John Doe"echo "Indirect reference: ${!name_var}"This script will output 「Indirect reference: John Doe」.
ConclusionParameter expansion with ${} in Bash is a powerful and versatile feature that allows you to manipulate and transform variables with ease. By mastering the various types of parameter expansions, you can write more efficient and flexible Bash scripts to accomplish a wide range of tasks. Take the time to familiarize yourself with these expansions and incorporate them into your scripting toolkit to become a true Bash expert.