Substring in Shell Script
Substring operations in Shell scripting allow you to extract, modify, and manipulate strings effectively. This tutorial provides step-by-step examples to help you master substring operations.
Get Substring from a String
You can extract a substring starting from a specific position using the syntax:
${string:position}
Example:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string:0}" # abcgauravabcxyz
echo "${string:1}" # bcgauravabcxyz
echo "${string:4}" # gauravabcxyz
Get Last n
Characters from a String
To extract the last n
characters of a string:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string: -3}" # xyz
Get Substring with Specific Length
You can extract a substring of a specific length starting from a given position:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string:0:3}" # abc
echo "${string:3:3}" # gau
Get Shortest Match from Starting
To remove the shortest match from the beginning of the string:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string#a*c}" # gauravabcxyz
Get Longest Match from Starting
To remove the longest match from the beginning of the string:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string##a*c}" # xyz
Get Shortest Match from the End
To remove the shortest match from the end of the string:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string%b*z}" # abcgaurava
Get Longest Match from the End
To remove the longest match from the end of the string:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string%%b*z}" # a
Replace First Occurrence of a Substring
To replace the first occurrence of a substring:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string/abc/xyz}" # xyzgauravabcxyz
Replace All Occurrences of a Substring
To replace all occurrences of a substring:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string//abc/xyz}" # xyzgauravxyzxyz
Remove First Occurrence of a Substring
To remove the first occurrence of a substring:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string/abc}" # gauravabcxyz
Remove All Occurrences of a Substring
To remove all occurrences of a substring:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string//abc}" # gauravxyz
Full Script Example
Here’s a complete example demonstrating all the above operations:
#!/bin/bash
string="abcgauravabcxyz"
echo "${string:0}"
echo "${string:1}"
echo "${string:4}"
echo "${string:0:3}"
echo "${string:3:3}"
echo "${string: -5}"
echo "${string#a*c}" # from starting, shortest match
echo "${string##a*c}" # from starting, longest match
echo "${string%b*z}" # from ending, shortest match
echo "${string%%b*z}" # from ending, longest match
string="abcgauravabcxyz"
echo "${string/abc/xyz}"
echo "${string//abc/xyz}"
echo "${string/abc}"
echo "${string//abc}"
Output
┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./substring.sh
abcgauravabcxyz
bcgauravabcxyz
gauravabcxyz
abc
gau
bcxyz
gauravabcxyz
xyz
abcgaurava
a
xyzgauravabcxyz
xyzgauravxyzxyz
gauravabcxyz
gauravxyz
Demo Video
Substring operations make string manipulation in Shell scripts easier and more flexible. Practice these examples to improve your scripting skills.