Syntax:
Some mostly used bash parameter expansion syntaxes are given below.
Parameter Expansion | Description |
${variable:-value} | If the variable is unset or undefined then expand the value. |
${variable:=value} | If the variable is unset or undefined then set the value to the variable. |
${variable:+value} | If the variable is set or defined then expand the value. |
${variable:start:length} | Substring will retrieve from the start position to length position of the variable. |
${variable:start} | Substring will retrieve from start position to the remaining part of the variable. |
${#variable} | Count the length of the variable. |
${variable/pattern/string} | Replace the part of the variable with string where the pattern match for the first time. |
${variable//pattern/string} | Replace all occurrences in the variable with string where all pattern matches. |
${variable/#pattern/string} | If the pattern exists at the beginning of the variable, then replace the occurrence with string. |
${variable/%pattern/string} | If the pattern exists at the end of the variable, then replace the occurrence with string. |
${variable#pattern} | Remove the shortest match from the beginning of the variable where the pattern matches. |
${variable##pattern} | Remove the longest match from the beginning of the variable where the pattern matches. |
${variable%pattern} | Remove the shortest match from the end of the variable where the pattern matches. |
${variable%%pattern} | Remove the longest match from the end of the variable where the pattern matches. |
Parameter expansion can be categorized by three groups. These are conditional parameter expansion, substring parameter expansion, and substitute parameter expansion. The uses of these parameter expansions are explained with examples in the next part of this tutorial.
Example-1: Conditional Parameter Expansion
These types of parameter expansions are used to check the variable is set or unset
The following command will check the variable, $myvar is set or unset. If $myvar is unset, then the string ‘bash’ will print.
The following command will print the value of $myvar if it is set.
The following command will set the value, ‘bash’ to $myvar and print ‘bash’ to the terminal if $myvar is unset.
Now, check the variable is set or unset by the following command.
The following command will print, ‘python’ to the terminal if $myvar is set before.
Again, Run the following command to check the current value of $myvar.
Output:
The following output will appear after running the above commands.
Example-2: Substring Parameter Expansion
Substring parameter expansion is used for various purposes, such as cut any portion of the string, count total characters of the string, etc. The string value can be cut in various ways. The uses of substring parameter expansions are shown in the next part of this tutorial.
The following command will assign “Bangladesh” to the variable, $mystr.
The following command will cut six characters from $mystr starting from position 0.
The following command will cut all characters from $mystr, starting from position 6.
The following command will count and print the total number of characters of $mystr.
Output:
The following output will appear after running the above commands.
Example-3: Substitute string using Parameter Expansion
Different types of parameter expansions can be used to substitute string value. The uses of parameter expansion for substituting the string value are shown in this part of the tutorial.
The following command will assign the value, “First In First Out” in the variable, $newstr.
The following parameter expansion will replace the string, “First” by the string “Last” of the variable, $newstr. Case-sensitive search will apply for this replacement.
The following parameter expansion will replace all occurrences of the string, “First” by the string “Last” of the variable, $newstr. Case-insensitive search will apply for this replacement.
Output:
The following output will appear after running the above commands.
The following command will assign the value, “Eat to live not live to eat” to the variable, $string.
The word, “eat” is appeared two times in the variable, $string. The following command will replace the word, “Eat” by “Work” that appears at the beginning of $string.
The following command will replace the word, “eat” by “work” that appears at the end of $string.
Output:
The following output will appear after running the above commands.
The following command will store the value “Web Programming Language” to the variable, $var.
The following parameter expansion will remove the word, “Web” from the beginning of the variable, $var.
The following parameter expansion will remove the word, “Language” from the end of the variable, $var.
Output:
The following output will appear after running the above commands.
Conclusion:
Bash parameter expansion is a very useful feature of Linux. It helps the Linux user to perform different types of string related operations very easily without any built-in function. Different types of string assignment, cutting string and replacement operations are shown in this tutorial by using bash parameter expansion. Hope, the reader will be able to perform string related tasks more efficiently by using parameter expansion after reading this tutorial.