Substring Replacement
Substring Replacement means replacing either the first match or all matches of the substring with the replacement string. The basic syntax for the same will be –
Basic Syntax:
${string/substring/replacement} #Replace first match of $substring with $replacement.
${string//substring/replacement} #Replace all matches of $substring with $replacement.
Below is an example of the same –
Example:
name="HeeksforHeeks" echo "Name is : ${name/Heek/Geek}" echo "Name is : ${name//Heek/Geek}"
Output:
Name is : GeeksforHeeks Name is : w3wiki
In the above example, the first one only replaces the first occurrence of Heek with Geek but the next statement changes all the occurrences of Heek with Geek. Below is the terminal shell depiction after executing the script –
Batch Script – Strings
A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one or more times as per our requirements. Here in this article, we are going to discuss the use of Strings within Bash Scripting. In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.
The simplest example of the use of Strings in Bash scripting can be given as –
name="Satyajit Ghosh" echo $name
Output:
Satyajit Ghosh
The above example shows a string-type variable name that prints something when called. So, the basic syntax for using Strings within a Bash Script will be –
Basic Syntax:
VariableName='value' echo $VariableName
Now, there are multiple ways present in Bash Scripting by which we can manipulate Strings. Let’s discuss them.