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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read