String Slicing & Substitution is the most commonly used method in any programming language, which split the values from either string or an array. In this section, I will cover only string slicing and will discuss array slicing later on in this article.
1
2
3
4
5
name="Yafiz Abraham"
echo ${name:3} # return everything except initial 3 characters
echo ${name:(-4)} # return 4 characters from end of string.
echo ${name:0:4} # return starting 4 characters from given string.
Substitution also kown as find & replace.
1
2
3
4
5
6
7
8
9
10
11
str="Hello World"
# find and replace only first matched characters or words
echo ${str/o/0} # ==>> Hell0 World
# find and replace all characters or words
echo ${str//o/0} # ==>> Hell0 W0rld
# a variable with a lot of spaces
str="Hello Yafiz Abraham"
echo ${str//+( )/-} # replace all spaces with single dash