String Manipulation

String operations and manipulations in Bash

String length and substring

String length, substring extraction, and prefix/suffix removal

#!/bin/bash
text="Hello World"

# String length
echo "Length: ${#text}"

# Substring extraction
echo "First 5 chars: ${text:0:5}"
echo "From position 6: ${text:6}"

# Remove prefix/suffix
filename="file.txt"
echo "Without extension: ${filename%.txt}"
echo "Without prefix: ${filename#file}"
String replacement

String replacement operations

#!/bin/bash
text="Hello World Hello"

# Replace first occurrence
echo "Replace first: ${text/Hello/Hi}"

# Replace all occurrences
echo "Replace all: ${text//Hello/Hi}"

# Replace at beginning
echo "Replace start: ${text/#Hello/Hi}"

# Replace at end
echo "Replace end: ${text/%Hello/Hi}"
Case conversion

String case conversion operations

#!/bin/bash
text="Hello World"

# Convert to lowercase
echo "Lowercase: ${text,,}"

# Convert to uppercase
echo "Uppercase: ${text^^}"

# Convert first character
echo "First char uppercase: ${text^}"
echo "First char lowercase: ${text,}"