Bash Working with Strings
Strings are sequences of characters — letters, numbers, spaces, and symbols. Bash provides many built-in ways to manipulate strings without needing any external tools. This topic covers the most practical string operations used in real-world scripts.
Defining Strings
#!/bin/bash name="eStudy247" greeting='Hello World' mixed="Learning Bash in 2026"
Single quotes vs Double quotes:
| Quote Type | Variable Expansion | Example | Result |
|---|---|---|---|
Double " " | Yes | "Hello $name" | Hello eStudy247 |
Single ' ' | No | 'Hello $name' | Hello $name (literal) |
String Length
Use ${#variable} to get the number of characters in a string.
#!/bin/bash
word="Bangalore"
echo "Length: ${#word}"
Output:
Length: 9
String Concatenation
Place two variables or strings next to each other to join them.
#!/bin/bash first="Good" second="Morning" combined="$first $second" echo $combined
Output:
Good Morning
Extracting a Substring
Use ${variable:start:length} to extract a portion of a string. Positions start at 0.
┌──────────────────────────────────────────┐ │ String: B a s h S c r i p t │ │ Index: 0 1 2 3 4 5 6 7 8 9 │ └──────────────────────────────────────────┘
#!/bin/bash
text="BashScript"
echo ${text:0:4} # Start at 0, take 4 chars
echo ${text:4:6} # Start at 4, take 6 chars
Output:
Bash Script
String Replacement
Replace the first occurrence of a pattern using ${variable/old/new}. Replace all occurrences using ${variable//old/new}.
#!/bin/bash
sentence="I love cats and cats love fish"
echo ${sentence/cats/dogs} # Replace first match
echo ${sentence//cats/dogs} # Replace all matches
Output:
I love dogs and cats love fish I love dogs and dogs love fish
Converting to Uppercase and Lowercase
#!/bin/bash
text="Hello World"
echo ${text^^} # Convert to UPPERCASE
echo ${text,,} # Convert to lowercase
echo ${text^} # Capitalize first letter only
Output:
HELLO WORLD hello world Hello World
Removing Parts of a String
Remove Prefix (from the beginning)
#!/bin/bash
filename="report_2026.csv"
echo ${filename#report_} # Remove shortest match from start
Output:
2026.csv
Remove Suffix (from the end)
#!/bin/bash
filename="report_2026.csv"
echo ${filename%.csv} # Remove .csv from end
Output:
report_2026
String Trim Summary Diagram
┌────────────────────────────────────────────────────────┐
│ String: "___hello___" │
│ │
│ ${var#pattern} → Remove shortest prefix match │
│ ${var##pattern} → Remove longest prefix match │
│ ${var%pattern} → Remove shortest suffix match │
│ ${var%%pattern} → Remove longest suffix match │
└────────────────────────────────────────────────────────┘
Checking if a String Contains a Substring
#!/bin/bash sentence="Bash scripting is powerful" if [[ "$sentence" == *"scripting"* ]]; then echo "Found the word 'scripting'" fi
Output:
Found the word 'scripting'
Checking if a String Starts or Ends with a Pattern
#!/bin/bash file="backup_2026.tar.gz" # Check start if [[ "$file" == backup* ]]; then echo "File is a backup" fi # Check end if [[ "$file" == *.gz ]]; then echo "File is compressed" fi
Output:
File is a backup File is compressed
Splitting a String
Use the IFS (Internal Field Separator) variable to split a string by a delimiter.
#!/bin/bash
data="John:30:Delhi"
IFS=':' read -ra parts <<< "$data"
echo "Name : ${parts[0]}"
echo "Age : ${parts[1]}"
echo "City : ${parts[2]}"
Output:
Name : John Age : 30 City : Delhi
Comparing Strings
#!/bin/bash a="apple" b="orange" if [ "$a" = "$b" ]; then echo "Same" elif [ "$a" \< "$b" ]; then echo "$a comes before $b alphabetically" else echo "$a comes after $b alphabetically" fi
Output:
apple comes before orange alphabetically
Common String Operations – Quick Reference
| Operation | Syntax |
|---|---|
| String length | ${#var} |
| Substring | ${var:start:length} |
| Replace first | ${var/old/new} |
| Replace all | ${var//old/new} |
| Uppercase | ${var^^} |
| Lowercase | ${var,,} |
| Remove prefix | ${var#prefix} |
| Remove suffix | ${var%suffix} |
Key Takeaways
- Use double quotes for strings when variable values need to appear inside them.
- Use
${#var}to get the string length. - Use
${var:start:len}to extract a part of the string. - Use
${var//old/new}to replace all occurrences of a pattern. - Use
${var^^}and${var,,}for case conversion. - Use
[[ "$var" == *"pattern"* ]]to check for substring matches.
