Bash Parameter Expansion

29/12/2020
The parameter is used in bash to store data. Different types of data can be stored in the parameter, such as integer, string, array, etc. by using built-in declare keyword.  The parameter can be a positional parameter, special parameter, and variable. Normally, ‘$’ symbol is used to print or assign the value of a variable, for example, ‘x=$y’. ‘$’ symbol is used for parameter expansion also that has various types of uses in bash. Parameter expansion can be used to modify, expand or replace the value of the parameter. The optional braces are used with the variable when using variable parameter expansion, such as ‘echo ${myvar}’. How parameter expansion can be used in bash for various purposes are shown in this tutorial.

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.

$ echo "${myvar:-bash}"

The following command will print the value of $myvar if it is set.

$ echo $myvar

The following command will set the value, ‘bash’ to $myvar and print ‘bash’ to the terminal if $myvar is unset.

$ echo "${myvar:=bash}"

Now, check the variable is set or unset by the following command.

$ echo $myvar

The following command will print, ‘python’ to the terminal if $myvar is set before.

$ echo "${myvar:+python}"

Again, Run the following command to check the current value of $myvar.

$ echo $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.

$ mystr="Bangladesh"

The following command will cut six characters from $mystr starting from position 0.

$ echo "${mystr:0:6}"

The following command will cut all characters from $mystr, starting from position 6.

$ echo "${mystr:6}"

The following command will count and print the total number of characters of $mystr.

$ echo "${#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.

$ newstr="First In First Out"

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.

$ echo "${newstr/Fast/Last}"

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.

$ echo "${newstr//Fast/Last}"

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.

$ string="Eat to live but not live to eat"

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.

$ echo "${string/#Eat/Work}"

The following command will replace the word, “eat” by “work” that appears at the end of $string.

$ echo "${string/%eat/work}"

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.

$ var="Web Programming Language"

The following parameter expansion will remove the word, “Web” from the beginning of the variable, $var.

$ echo "${var/#Web}"

The following parameter expansion will remove the word, “Language” from the end of the variable, $var.

$ echo "${var/%Language}"

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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Convert Hexadecimal to Decimal in Bash

Four types of number systems are popular in computer systems. These are Decimal, Binary, Octal and Hexadecimal. The binary...
29/12/2020

Using and Customizing Bash Command History

The bash shell is the default command line environment available in most Linux distributions. Similar to all shell environments,...
29/12/2020

Bash Loop Through a List of Strings

A list of strings or array or sequence of elements can be iterated by using for loop in bash. How you can iterate the list...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024