Javatpoint Logo
Javatpoint Logo

Bash Substring

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 Javatpoint." is a substring of "We welcome you on Javatpoint."

Syntax

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:

where,

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

Output

Bash Substring

Example 2: To Extract from Specific Character onwards

Output

you on Javatpoint.

Here, end of the string is considered as the end of the substring.

Example 3: To Extract a Single Character

Output

y

Example 4: To Extract the specific characters from last

Output

Javatpoint.

Conclusion

In this topic, we demonstrated how to extract a substring from a string in bash scripting with different types of scenarios and examples.






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA