This strategy examines some simple text substitution techniques and the parameter extensions available in bash, and these simple techniques may be useful in some ways.
Below, I will explain this part in the form of an instance.
Replace part of the text in a variable:
The commands you use are as follows:
Var= "This is a line of text"
Echo ${var/line/replaced}
The contents of the output are as follows:
This is a replaced of text
We can see line being replaced by replaced.
We can generate substrings by specifying the starting position and length of the string, as follows:
This is a replaced of text
We can see line being replaced by replaced.
We can generate substrings by specifying the starting position and length of the string, as follows:
string= "ABCDEFGHIJKLMNOPQRSTUVXXYZ"
Echo ${string:4}
The output results are as follows:
Efghijklmnopqrstuvxxyz
Print the eighth character starting with the fifth character:
Echo ${string:4:8}
The output results are:
Efghijkl
The index of the starting character is counted from 0, and we can count the last character index to 1. However, if you use a negative number as an index, you must enclose the negative number in parentheses, for example (-1) is the index of the last character.
echo ${string: (-1)}
The output results are:
Z
echo ${string: (-2): 2}
The output results are:
Yz
Here's a screenshot of the demo: