Table of contents
Bash parameter expansion allows you to manipulate portions of parameters, which are variables or strings. It is a useful feature to transform strings in various ways.
Some common parameter expansions are:
${parameter}: Simply refer to the value of a parameter. For example:
name="John" echo ${name} # Prints John
${parameter:offset}: Expands to the portion of the parameter starting at the specified offset. For example:
name="John Doe" echo ${name:3} # Prints Doe
${parameter:offset:length}: Expands to a substring of the parameter with the specified length, starting at the offset. For example:
name="John Doe" echo ${name:3:4} # Prints Doe
${#parameter}: Expands to the length of the parameter. For example:
name="John Doe" echo ${#name} # Prints 8
${parameter/pattern/string}: Performs pattern replacement on the first match of pattern. For example:
name="John Doe" echo ${name/Jo/T} # Prints Tohn Doe
${parameter//pattern/string}: Performs pattern replacement on all matches of pattern.
So in summary, bash parameter expansion allows you to manipulate strings and portion of parameters in various useful ways, making Bash scripting more powerful.
What is Expansion?
In Bash, expansion refers to the process of evaluating a string to produce a new value. There are several types of expansions in Bash:
Parameter Expansion: As I mentioned earlier, parameter expansion involves manipulating parameters (variables) and strings. It allows you to extract portions of parameters, perform pattern replacements, and get the length of parameters.
Tilde Expansion: This expands a tilde (~), followed by an optional user and/or hostname, to the home directory of that user. For example, ~ expands to the current user's home directory.
Command Substitution: This allows the output of a command to be substituted in place. It is done using either
command
or $(command). For example:foo=$(ls) # Expands to the output of 'ls'
Arithmetic Expansion: This allows performing arithmetic on variables. It is done using $((expression)). For example:
count=1 echo $((count + 1)) # Prints 2
Brace Expansion: This performs text replacement using braces {}. For example:
echo {a,b,c}{1..3} # Prints a1 a2 a3 b1 b2 b3 c1 c2 c3
Word Splitting: This breaks a string into words using the characters in $IFS (default is space, tab, and newline).
So in summary, expansion refers to evaluating a string and producing a new value. Bash supports various types of expansions to provide flexibility and power to shell scripts. Parameter expansion is one of the most useful and commonly used expansions.
Use-Cases
Here are some interesting Bash scripts that demonstrate various expansion use cases:
Printing a range of numbers:
#!/bin/bash for i in {1..10} do echo $i done
This uses brace expansion to print numbers from 1 to 10.
Generating file names with a pattern:
#!/bin/bash for i in {a..z} do touch file_$i.txt done
This uses brace expansion to generate 26 files named file_a.txt to file_z.txt.
Getting command output and using it:
#!/bin/bash read -p "Enter directory name: " dir files=$(ls $dir) for f in $files do echo "File name is $f" done
This uses command substitution to get the output of ls $dir
and store it in the files
variable.
Performing arithmetic on variables:
#!/bin/bash count=10 count=$((count + 5)) echo $count # Prints 15
This uses arithmetic expansion to add 5 to the count
variable.
Extracting part of a string:
#!/bin/bash string="Hello World" echo ${string:0:5} # Prints Hello
This uses parameter expansion to extract the first 5 characters of the string
variable.
Disclaim: This article is generated with AI (Rix). Hope these examples give you some ideas on using different expansion techniques in your Bash scripts! Feel free to comment and add new examples created by yourself.