In this topic, we have explained how to compute substring of a given string.
A substring is a sequence of characters within a string. Bash provides an option to extract the information from a string itself. You can extract the digits or a given string using several methods.
For example, “welcome you on tutoraspire.” is a substring of “We welcome you on tutoraspire.”
The command for the extraction of substring is a build-in bash command, and so it is very good to use for performance perspective.
The syntax of the substring extraction can be defined as:
Variable is the variable name that contains a string.
Offset is used to specify the position from where to start the extraction of a string.
Length is used to specify the range of the characters to be executed from the offset.
Note: Assigning length is optional. If length is not provided, then end of the string will be considered as the end of the substring.
Let’s understand the concept of extracting a substring from the string with the help of some examples:
Example 1: To Extract till Specific Characters from Starting
Example 2: To Extract from Specific Character onwards
you on tutoraspire.
Here, end of the string is considered as the end of the substring.
Example 3: To Extract a Single Character
Example 4: To Extract the specific characters from last
In this topic, we demonstrated how to extract a substring from a string in bash scripting with different types of scenarios and examples.