In this tutorial, you will learn the technique that will help you in getting substring of a string. Following is the syntax that you must follow in order to get a substring:
${variablename:position:length}
The above syntax returns a substring that starts from the position and goes till the number of chracters specified by the length
parameter.
The first character of $variablename
is at position 0. Here length is optional. If you do not specify the length
then it will return a substring that starts at position
and goes till the end of $variablename
.
mystr="BashShellScripting" substr=${mystr:9:6} echo "Substring is $substr"
Output of the above program
Substring is Script
mystr="BashShellScripting" substr=${mystr:4} echo "Substring is $substr"
Output of the above program
Substring is ShellScripting