String Operations
Bash provides powerful built-in string manipulation capabilities. Learn to extract, measure, and transform strings without external tools.
String Length
Get the length of a string:
#!/bin/bash
STR="Hello, World!"
echo ${#STR} # 13
# Works with variables
NAME="Alice"
echo "Name has ${#NAME} characters" # 5
Substring Extraction
Extract parts of a string:
#!/bin/bash
STR="Hello, World!"
# ${string:position}
echo "${STR:7}" # World!
# ${string:position:length}
echo "${STR:0:5}" # Hello
echo "${STR:7:5}" # World
# Negative position (from end)
echo "${STR: -6}" # World!
echo "${STR: -6:5}" # World
Note: Space before negative number is required!
Exercise: Extract Substring
Get part of a string:
String Replacement
Replace parts of strings:
#!/bin/bash
STR="hello world world"
# Replace first occurrence
echo "${STR/world/universe}" # hello universe world
# Replace all occurrences
echo "${STR//world/universe}" # hello universe universe
# Replace at beginning
echo "${STR/#hello/hi}" # hi world world
# Replace at end
echo "${STR/%world/earth}" # hello world earth
Removing Substrings
Remove from beginning or end:
#!/bin/bash
FILE="archive.tar.gz"
# Remove shortest match from beginning
echo "${FILE#*.}" # tar.gz
# Remove longest match from beginning
echo "${FILE##*.}" # gz
# Remove shortest match from end
echo "${FILE%.*}" # archive.tar
# Remove longest match from end
echo "${FILE%%.*}" # archive
Memory Trick
#removes from the beginning (# is before $ on keyboard)%removes from the end (% is after numbers)- Single = shortest match, Double = longest match
Exercise: Extract Filename
Get filename from path:
Case Conversion
Change string case (Bash 4+):
#!/bin/bash
STR="Hello World"
# Lowercase
echo "${STR,,}" # hello world
echo "${STR,}" # hello World (first char only)
# Uppercase
echo "${STR^^}" # HELLO WORLD
echo "${STR^}" # Hello World (first char only)
# Toggle case (Bash 4+)
echo "${STR~~}" # hELLO wORLD
Default Values
Provide defaults for empty/unset variables:
#!/bin/bash
# Use default if unset or empty
echo "${NAME:-Unknown}" # Unknown if NAME is empty
# Assign default if unset or empty
echo "${NAME:=Default}" # Sets NAME to Default
# Display error if unset or empty
echo "${NAME:?Error: NAME is required}"
# Use alternative if set
echo "${NAME:+Name is: $NAME}"
| Syntax | Meaning |
|---|---|
${VAR:-default} | Use default if VAR is unset/empty |
${VAR:=default} | Assign default if VAR is unset/empty |
${VAR:?error} | Exit with error if VAR is unset/empty |
${VAR:+value} | Use value if VAR is set |
Concatenation
Join strings together:
#!/bin/bash
FIRST="Hello"
LAST="World"
# Simple concatenation
FULL="$FIRST $LAST"
echo "$FULL" # Hello World
# Concatenation with braces
PREFIX="pre"
echo "${PREFIX}fix" # prefix
# Append to variable
STR="Hello"
STR+=" World"
echo "$STR" # Hello World
Exercise: String Default
Use default value:
Indirect Variable Reference
Access variable by name:
#!/bin/bash
VAR_NAME="greeting"
greeting="Hello, World!"
# Using indirect reference
echo "${!VAR_NAME}" # Hello, World!
# List all matching variables
echo "${!BASH*}" # All vars starting with BASH
String Splitting
Split into array:
#!/bin/bash
STR="one,two,three"
# Using IFS
IFS=',' read -ra PARTS <<< "$STR"
echo "${PARTS[0]}" # one
echo "${PARTS[1]}" # two
echo "${PARTS[2]}" # three
# Loop through parts
for part in "${PARTS[@]}"; do
echo "Part: $part"
done
Quick Reference
# Length
${#string}
# Substring
${string:start}
${string:start:length}
# Replacement
${string/old/new} # First
${string//old/new} # All
${string/#old/new} # Beginning
${string/%old/new} # End
# Removal
${string#pattern} # Beginning (shortest)
${string##pattern} # Beginning (longest)
${string%pattern} # End (shortest)
${string%%pattern} # End (longest)
# Case
${string,,} # Lowercase
${string^^} # Uppercase
# Defaults
${string:-default} # Use default
${string:=default} # Assign default
Key Takeaways
${#VAR}gives string length${VAR:start:length}extracts substrings${VAR/old/new}replaces first match,//replaces all#removes from beginning,%removes from end${VAR,,}lowercases,${VAR^^}uppercases${VAR:-default}provides default for empty variables- Use
+=to append to strings - These are all built-in - no external commands needed!

