Syntax:
The syntax of Bash substring extraction is as follows:
Here, the variable contains a string value. The offset contains the starting position of the main string from where the substring is extracted and the value of the offset can be any positive or negative integer. The length contains the total length of the substring that is extracted.
Different Examples of Substring Extraction
The different ways of extracting the substring after the specified position of the main string are shown in this part of the tutorial.
Example 1: Extract the Substring from the Start of the String
Create a Bash file with the following script that extracts a substring from a string of multiple words using a positive starting position and length. The starting position is set to 0 and the substring length is set to 4 in the script.
#Define a string variable
mainStr="Bash is a popular programming language"
#Print the string variable
printf "The main string value:\n$mainStr"
#Create a substring by extracting from the first character
#of the main string and the length of the substring will be 4
subStr="${mainStr:0:4}"
#Print the substring value
printf "\n\nThe substring value:\n$subStr\n"
The following output appears after executing the script. The substring value is “Bash” if the substring is extracted from the 0 positions with the length of 4 from the string value, “Bash is a popular programming language”.
Example 2: Extract the Substring from the Middle of the String
Create a Bash file with the following script that extracts a substring from a string of multiple words using the positive starting position that is greater than 0 and the positive length value. The starting position is set to 10 and the substring length is set to 7 in the script.
#Define a string variable
mainStr="Bash is a popular programming language"
#Print the string variable
printf "The main string value:\n$mainStr"
#Create a substring by extracting from the 10th position
#of the main string and the length of the substring will be 7
subStr="${mainStr:10:7}"
#Print the substring value
printf "\n\nThe substring value:\n$subStr\n"
The following output appears after executing the script. The substring value is “popular” if the substring is extracted from the 10 positions with the length of 7 from the string value, “Bash is a popular programming language”.
Example 3: Extract the Substring with the Positive Starting Position Only
Create a Bash file with the following script that extracts a substring from a string of multiple words using a positive starting position only. The starting position is set to 18 in the script. If no length value is set, the remaining part of the string is extracted from the starting position as a substring.
#Define a string variable
mainStr="Bash is a popular programming language"
#Print the string variable
printf "The main string value:\n$mainStr"
#Create a substring by extracting from the after the
#18th position of the main string
subStr="${mainStr:18}"
#Print the substring value
printf "\n\nThe substring value:\n$subStr\n"
The following output appears after executing the script. The substring value is “programming language” if the substring is extracted from the 18th position with no length value from the string value, “Bash is a popular programming language”.
Example 4: Extract the Substring with the Negative Starting Position Only
Create a Bash file with the following script that extracts a substring from a string of multiple words using a negative starting position only. The starting position is set to -8 in the script. The starting position is counted from the right side of the string and the counting starts from 1 if a negative starting position is used.
#Define a string variable
mainStr="Bash is a popular programming language"
#Print the string variable
printf "The main string value:\n$mainStr"
#Create a substring by extracting the last 8 characters
#from the main string
subStr="${mainStr:(-8)}"
#Print the substring value
printf "\n\nThe substring value:\n$subStr\n"
The following output appears after executing the script. The substring value is “language” if the substring is extracted from the -8 position from the string value, “Bash is a popular programming language”.
Example 5: Extract Substring with the Negative Starting Position and Positive Length
Create a Bash file with the following script that extracts a substring from a string of multiple words using a negative starting position and a positive length. The starting position is set to -20 and the length is set to 11 in the script.
#Define a string variable
mainStr="Bash is a popular programming language"
#Print the string variable
printf "The main string value:\n$mainStr"
#Create a substring by extracting from the 20th position and
#from the right side of the main string and the length of the
#string is 11 characters
subStr="${mainStr:(-20):11}"
#Print the substring value
printf "\n\nThe substring value:\n$subStr\n"
The following output appears after executing the script. The substring value is “programming” if the substring is extracted from the -20 position with the length of 11 from the string value, “Bash is a popular programming language”.
Conclusion
Various ways of extracting the substring from a string data using the Bash script are shown in this tutorial using simple examples.