String Manipulation in Bash on Linux

Bash is a shell language used in Linux systems that allows users to interact with the system through a command-line interface. Bash offers several string manipulation capabilities that can help users manipulate and process text strings effectively.

Basic String Operations

Bash provides fundamental 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 a string variable, you can use the echo command

echo $mystring

The output will be

Hello, world!

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

echo ${#mystring}

The output will be

13

To concatenate two strings, you can place them adjacent to each other

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 using parameter expansion.

Pattern Replacement

To substitute the first occurrence of a pattern with another string, use the ${variable/pattern/replacement} syntax

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

The output will be

Hello, John!

To substitute all occurrences of a pattern with another string, use the ${variable//pattern/replacement} syntax

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

The output will be

Hi, world! Hi, John!

To delete all occurrences of a pattern, leave the replacement part empty

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

The output will be

, world! , John!

String Slicing

Bash allows users to extract a substring from a larger string using the ${variable:start:length} syntax.

To extract a substring from the beginning of a string

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

The output will be

Hello

To extract a substring from the end of a string, use negative indexing

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

The output will be

world!

String Comparison

Bash provides several operators for comparing strings within conditional statements.

Equality Testing

To check if two strings are equal, use the == 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 the != 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

Regular Expression Pattern Matching

Bash provides support for regular expressions using the =~ operator within double brackets.

Pattern Matching

To perform pattern matching using regular expressions

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

Capturing Groups

To extract substrings using capturing groups with the BASH_REMATCH array

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

Advanced String Manipulation

Using sed for Complex Substitutions

The sed command provides powerful pattern replacement capabilities

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

The output will be

Hello, Universe!

String Manipulation in Loops

String operations can be performed within loops for batch processing

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

This code loops through all .txt files and renames them with a .bak extension, where ${file%.txt} removes the .txt extension from the filename.

Conclusion

Bash provides comprehensive string manipulation capabilities including basic operations, pattern substitution, slicing, and regular expression support. These tools enable efficient text processing and automation tasks on Linux systems. Mastering these techniques allows for powerful command-line scripting and system administration tasks.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements