String Manipulation in Bash on Linux


Bash is a shell language used in Linux systems that allows users to interact with system through command-line interface. Bash offers several string manipulation capabilities that can help users manipulate and process text strings. In this article, we will explore various techniques available for string manipulation in Bash.

Basic String Operations

Bash provides basic operations for manipulating strings. To create a string variable in Bash, you simply assign a value to a variable name −

mystring="Hello, world!"

To display contents of string variable, you can use echo command −

echo $mystring

The output will be −

Hello, world!

To get length of a string, use ${#} syntax −

echo ${#mystring}

The output will be −

13

To concatenate two strings, use ${} syntax −

string1="Hello,"
string2=" world!"
echo ${string1}${string2}

The output will be −

Hello, world!

String Substitution

Bash provides various techniques to substitute parts of a string with another string.

Substring Substitution

To substitute first occurrence of a substring with another string, use ${} syntax −

mystring="Hello, world!"
echo ${mystring/world/John}

The output will be −

Hello, John!

To substitute all occurrences of a substring with another string, use ${} syntax with // operator −

mystring="Hello, world! Hello, John!"
echo ${mystring//Hello/Hi}

The output will be −

Hi, world! Hi, John!

To delete all occurrences of a substring, use ${} syntax with // operator −

mystring="Hello, world! Hello, John!"
echo ${mystring//Hello/}

The output will be −

, world! , John!

Regular Expression Substitution

Bash also supports regular expression substitution. To substitute first occurrence of a regular expression with another string, use ${} syntax with / operator −

mystring="Hello, world!"
echo ${mystring/[Hh]ello/Hi}

The output will be −

Hi, world!

To substitute all occurrences of a regular expression with another string, use ${} syntax with // operator −

mystring="Hello, world! hello, John!"
echo ${mystring//[Hh]ello/Hi}

The output will be −

Hi, world! Hi, John!

To delete all occurrences of a regular expression, use ${} syntax with // operator and an empty string as replacement −

mystring="Hello, world! hello, John!"
echo ${mystring//[Hh]ello/}

The output will be −

, world! , John!

String Slicing

Bash allows users to extract a substring from a larger string using ${} syntax with : operator.

To extract a substring from beginning of a string, use ${:n} syntax −

mystring="Hello, world!"
echo ${mystring:0:5}

The output will be −

Hello

To extract a substring from end of a string, use ${: -n} syntax −

mystring="Hello, world!"
echo ${mystring: -6}

The output will be −

world!

String Comparison

Bash provides several techniques for comparing strings.

Equality and Inequality

To check if two strings are equal, use == operator −

string1="Hello, world!"
string2="Hello, world!"
if [ "$string1" == "$string2" ]; then
   echo "Strings are equal"
else
   echo "Strings are not equal"
fi

The output will be −

Strings are equal

To check if two strings are not equal, use != operator −

string1="Hello, world!"
string2="Hello, John!"
if [ "$string1" != "$string2" ]; then
	echo "Strings are not equal"
else
	echo "Strings are equal"
fi

The output will be −

Strings are not equal

Greater than and less than

Bash also supports string comparison based on alphabetical order. To check if one string is greater than another, use > operator −

string1="abc"
string2="def"
if [ "$string1" > "$string2" ]; then
   echo "string1 is greater than string2"
else
   echo "string2 is greater than string1"
fi

The output will be −

string2 is greater than string1

To check if one string is less than another, use < operator −

string1="abc"
string2="def"
if [ "$string1" < "$string2" ]; then
   echo "string1 is less than string2"
else
   echo "string2 is less than string1"
fi

The output will be −

string1 is less than string2

Regular Expressions

Bash provides support for regular expressions in string manipulation. Regular expressions allow users to search for and manipulate text based on patterns.

Pattern Matching

To perform a pattern match using regular expressions, use =~ operator −

mystring="Hello, world!"
if [[ $mystring =~ ^Hello ]]; then
   echo "String starts with Hello"
else
   echo "String does not start with Hello"
fi

The output will be −

String starts with Hello

Substring Extraction

To extract a substring based on a regular expression pattern, use ${} syntax with =~ operator −

mystring="Hello, world!"
if [[ $mystring =~ ([A-Za-z]+), ]]; then
   echo "Match found: ${BASH_REMATCH[0]}"
   echo "First group: ${BASH_REMATCH[1]}"
else
   echo "No match found"
fi

The output will be −

Match found: Hello,
First group: Hello

String Concatenation

In Bash, concatenation of strings can be performed using + operator. two strings to be concatenated are placed adjacent to each other, without any delimiter between them.

string1="Hello"
string2="world"
concatenated_string="$string1$string2"
echo $concatenated_string

The output will be −

Helloworld

String Length

To get length of a string in Bash, use ${#} syntax. For example −

string="Hello, world!"
length=${#string}
echo $length

The output will be −

13

String Substitution Using sed

Bash also provides sed (stream editor) for substituting patterns within a string. sed command can be used with pipes to modify a string and display output.

For example, to replace all occurrences of "world" in a string with "Universe", following command can be used −

echo "Hello, world!" | sed 's/world/Universe/g'

The output will be −

Hello, Universe!

String Manipulation in Loops

String manipulation can also be performed in loops in Bash. For example, to loop through a list of files with ".txt" extension and rename them by appending ".bak" to filename, following code can be used −

for file in *.txt
do
	mv "$file" "${file%.txt}.bak"
done

In above code, ${file%.txt} represents filename without ".txt" extension, and ".bak" is appended to it.

Conclusion

In this article, we have explored various techniques for string manipulation in Bash on Linux. Bash provides several capabilities for string operations, including basic operations, string substitution, string slicing, and string comparison. Additionally, Bash supports regular expressions for pattern matching and substring extraction. With these tools, users can manipulate and process text strings to accomplish a variety of tasks on Linux command-line interface.

Updated on: 24-Mar-2023

666 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements