Parameter Expansion and String Manipulation
Parameter Expansion and String Manipulation
Function Usage in Shell
Parameter expansion is a fundamental technique in shell scripting, enabling developers to manipulate variables and strings by substituting values, trimming whitespace, and performing pattern matching. This process is critical for building dynamic and flexible shell scripts, as it allows for precise control over data transformation and processing.
Parameter Expansion
Parameter expansion involves substituting parts of a variable with other values, often using syntax like ${var}. This allows for dynamic value retrieval and transformation.
Basic Concepts
Parameter expansion is based on the idea of substituting placeholders in variables. For example:
var="hello world"
echo ${var}
This outputs hello world, with the variable var expanded to its stored value.
Common Expansions
- ${var}: Expands the variable to its value.
- ${var#pattern}: Removes the shortest match of
patternfrom the beginning ofvar. - ${var##pattern}: Removes the longest match of
patternfrom the beginning ofvar. - ${var%pattern}: Removes the shortest match of
patternfrom the end ofvar. - ${var%%pattern}: Removes the longest match of
patternfrom the end ofvar. - ${var%/}: Removes the last slash from
var. - ${var#*}: Removes all occurrences of
*from the beginning ofvar. - ${var##*}: Removes all occurrences of
*from the beginning ofvar. - ${var% *}: Removes the first occurrence of
*from the end ofvar. - ${var%% *}: Removes the last occurrence of
*from the end ofvar.
These expansions are often used to refine or filter variable values. For example:
name="john.doe"
echo ${name#*.} # Outputs "john.doe"
echo ${name##*.} # Outputs "john.doe"
Advanced Expansions
- ${var:-default}: Substitutes
varwithdefaultifvaris empty or null. - ${var:+flag}: Substitutes
flagifvaris non-empty. - ${var:?error}: Displays an error message if
varis empty or null. - ${var@}: Expands
varto a list of its components (e.g.,a b cforvar="a b c"). - ${var%/}: Removes the last slash from
var. - ${var%/prefix}: Removes the last occurrence of
prefixfromvar.
These advanced expansions provide flexibility for handling complex data structures.
String Manipulation
String manipulation involves modifying or extracting parts of a string using shell commands and expansions. Key techniques include substitution, trimming, concatenation, and pattern matching.
Substitution
Substitution replaces parts of a string with other values. For example:
str="apple,banana,orange"
echo ${str,,} # Outputs "apple,banana,orange" (lowercase)
echo ${str,,} # Outputs "apple,banana,orange" (lowercase)
The ${str,,} expansion converts all characters to lowercase.
Trimming
Trimming removes leading and trailing whitespace. For example:
str=" hello world "
echo ${str#} # Outputs "hello world"
echo ${str##} # Outputs "hello world"
The ${str#} expansion removes leading whitespace, while ${str##} removes trailing whitespace.
Concatenation
Concatenation joins strings together. For example:
a="hello"
b="world"
echo "${a} ${b}" # Outputs "hello world"
This combines a and b into a single string.
Pattern Matching
Pattern matching allows for conditional checks. For example:
str="apple"
echo "${str##*}"; # Outputs "apple"
echo "${str%%*}"; # Outputs "apple"
The ${str##*} expansion removes all occurrences of * from the beginning of str.
Common Commands
Shell scripts often leverage built-in commands to perform parameter expansion and string manipulation.
- awk: Processes text lines and fields, allowing for pattern matching and substitution.
- sed: Edits text using stream editors, supporting complex substitutions.
- bash: Provides a rich set of shell functions for expansion and manipulation.
These tools are essential for creating robust and efficient shell scripts.
Conclusion
Parameter expansion and string manipulation are indispensable tools in shell scripting, enabling precise control over variables and data processing. By mastering techniques such as substitution, trimming, and pattern matching, developers can build dynamic and flexible shell scripts. Understanding these concepts allows for the creation of powerful tools that handle complex data transformations efficiently.