Bash String is a data type similar to integer or boolean. It is generally used to represent text. It is a string of characters that may also contain numbers enclosed within double or single quotes.
Example: "geeksforgeeks", "Geeks for Geeks" or "23690" are strings
Creating a String
A basic declaration and assignment of string
str1="GeeksforGeeks"
str2=geeksforgeeks
The quotes can be omitted, but there shouldn't be any space before and after the equals operator
Example
#!/bin/bash
#Initializing the strings
string1="GeekForGeeks"
string2='Geeks for Geeks'
string3=2345656778
#Printing the strings
echo $string1
echo $string2
echo $string3
Output
Reading the string from the user
The input can be read from the user using the read command.
Example
#!/bin/bash
echo What is your name
read name
echo "Hello there, $name"
Output
Concatenation of Strings
Strings in bash can be easily concatenated by listing the strings in order.
Example
#!/bin/bash
s1="Geeks"
s2="for"
s3="Geeks"
s4=${s1}${s2}${s3};
echo ${s4};
Output
Length of the String
The length of a given string can be accessed using the # operator by placing it inside the parameter expansion (curly braces) before the variable name.
Example
#!/bin/bash
string="geeksforgeeks"
echo "The length of the string is : ${#string}"
Output
String Replacement
Parts of an existing string can be replaced using / inside the parameter expansion. The syntax is given below.
${<variable_name>/<string_to_replace>/<new_string>}
This will replace the specified string with the new string once.
Example
string="Hello, Geeks!"
echo "String before replacement ${string}"
echo "String after replacement ${string/Geeks/World}"
Output
To replace every single matched string from the existing string, two slashes are used '//' after the variable name, so it becomes.
${<variable_name>//<string_to_replace>/<new_string>}
Let us replace all the instances of the word butter from the below phrase.
Betty brought a bit of butter
but the butter was bit of bitter
so she brought some better butter
to make the bitter butter better
#!/bin/bash
para="Betty brought a bit of butter but the butter was bit of bitter so she brought some better butter to make the bitter butter better"
#This will replace every instance of butter in the phrase to BATTER
echo "${para//butter/BATTER}"
Output
Similar Reads
Bash Scripting - Substring sIn this article, we will discuss how to write a bash script to extract substring from a string. Extracting an Index-Based Substring There are various ways to obtain substring based on the index of characters in the string: Using cut commandUsing Bash substringUsing expr substr commandUsing awk comm
4 min read
Bash Scripting - Split String In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met
4 min read
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 u
4 min read
Batch Script - Mid String In this article , we are going to learn how to use the concept of Mid String using Batch Script. Using the concept of 'Mid String' we are extracting out a sub string between two indices of any given string. Batch Script :@echo off set str=GeeksforGeeks echo %str% set str=%str:~5,-5% echo %str% pause
2 min read
Batch Script - Left String In this article, we are going to study Left string in Batch Script. In Batch Script, the Left string is used to extract the characters from the beginning of the string by giving position 0 and a length using:~ while expanding a variable content. Example 1: Batch script set str=GeeksForGeeks echo.%st
1 min read
Batch Script - Right String In this article, we are going to learn how to use the concept of Right String using Batch Script. Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main strin
2 min read
Shell Scripting - Here Strings Here String is a kind of string that is used for input redirection from text or a variable. In here string Input will be mentioned in the same line within the quotes. the string inside the quotes is known as the Here string and Its command contains "<<<" (3 less than the symbol). Here strin
2 min read
Batch Script - String length In this article , we are going to learn how to find length of any String using Batch Script. Batch Script :@echo off set str=Geeks For Geeks call :strLen str strlen echo String is %strlen% characters long pause exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set
2 min read
Batch Script - Create String In this article, we are going to learn how to create a String using Batch Script. Batch Script :@echo off set str=Geeks for Geeks echo %str% pauseBy using ' set ' we are getting input of any string.Ex: set str=input stringIn the next line, we are using ' echo %str% ' for printing our input string.Th
1 min read
Bash Scripting - Working of Bash Scripting Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command l
6 min read