Open In App

Batch Script – Strings

Last Updated : 01 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

String Length

If we want to get or print the length of a String, we can use the following syntax for the same –

Basic Syntax:

variableName=value
echo ${#variablename}

Below is an example of the same –

Example:

name="Satyajit Ghosh"
echo The size of the String : ${#name}

Output:

The size of the String : 14

Here the size of the string is displayed on the output. Below is the terminal shell depiction after executing the script –

 

Substring Extraction

We can extract a substring or part of a String. We can provide only starting positions, both starting and ending positions, or can use right-ended indexing. The basic syntax for the same will be –

Basic Syntax:

${string:startindex:endindex} #both starting and ending position

${string: right-ended-index} #using right-ended indexing

${string:startindex} #only starting position

Below is an example of the same –

Example:

name=”Satyajit Ghosh”

# using starting and ending position

echo The First Name is : ${name:0:8}

# using right-ended indexing

echo The Last Name is  : ${name: -5} #added-space required

# using only starting position

echo The Last Name is  : ${name:8}

Output:

The First Name is : Satyajit
The Last Name is : Ghosh
The Last Name is : Ghosh

Below is the terminal shell depiction after executing the script –

 

Substring Removal

It deletes the shortest or longest match of a substring from the front of a string. The basic syntax for the same will be –

Basic Syntax:

# Deletes shortest match of 
# $substring from front of $string.
${string#substring} 

# Deletes longest match of 
# $substring from front of $string.
${string##substring} 

Below is an example of the same –

Example:

name="GeeksforGeeks"
echo "Name is : ${name#G*k}"
echo "Name is : ${name##G*k}"

Output:

Name is : sforGeeks
Name is : s

In the above example, the shortest match between G and k is ‘Geek’ and the longest match is ‘GeeksforGeek‘, so the matched portion is removed and we have the rest of the letters present in the output. Below is the terminal shell depiction after executing the script –

 

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 : GeeksforGeeks

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 –

 

String Concatenation

In bash scripting, we can concatenate two or more strings together. The basic syntax for the same will be –

Basic Syntax:

string=${string1}${string2}${string3}
or
string=$string1$string2$string3
or
string="$string1""$string2""$string3"

Below is an example of the same –

Example:

s1="Satyajit"
s2=" "
s3="Ghosh"

#String concatenation
name=$s1$s2$s3

echo $name

Output:

Satyajit Ghosh

In the above example, we have concatenated first name, last name, and a space in between. Then displayed it. Below is the terminal shell depiction after executing the script –

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads