Skip to main content

Shell String Operations Complete Guide

Published: April 24, 2016 Updated: May 25, 2026 Larry Qu 3 min read

Overview

String operations in Shell scripting are essential skills for daily operations and automation.

Getting String Length

Basic Method

# Using ${#VAR}
VAR="hello"
echo ${#VAR}
# Output: 5

# Also works directly
echo ${#"hello"}
# Output: 5

Methods in Different Shells

# Bash
VAR="hello"
echo ${#VAR}        # 5
echo ${#VAR[0]}     # array

# expr command
VAR="hello"
expr length "$VAR"
# Output: 5

# wc command
VAR="hello"
echo -n "$VAR" | wc -c
# Output: 5

# awk
VAR="hello"
echo "$VAR" | awk '{print length}'
# Output: 5

String Substring

1. Substring from Left

VAR="Hello World"

# ${VAR:start} - From the start-th character to end (0-indexed)
echo ${VAR:1}
# Output: ello World

# ${VAR:start:length} - From start, take length characters
echo ${VAR:0:5}
# Output: Hello

# ${VAR: -length} - From the right, take length characters (note space after colon)
echo ${VAR: -5}
# Output: World

2. Using expr command

VAR="Hello World"

# expr substr $VAR start length
expr substr "$VAR" 1 5
# Output: Hello

# start is 1-indexed

3. Using cut command

VAR="Hello World"

# Take first 5 characters
echo "$VAR" | cut -c1-5
# Output: Hello

# Take from 7th character to end
echo "$VAR" | cut -c7-
# Output: World

String Replacement

1. Replace First Match

VAR="Hello World World"

# ${VAR/pattern/replacement}
echo ${VAR/World/Shell}
# Output: Hello Shell World

2. Replace All Matches

VAR="Hello World World"

# ${VAR//pattern/replacement}
echo ${VAR//World/Shell}
# Output: Hello Shell Shell

3. Replace at Beginning or End

VAR="Hello World"

# Replace match at beginning
echo ${VAR/#Hello/Hi}
# Output: Hi World

# Replace match at end
echo ${VAR/%World/Earth}
# Output: Hello Earth

4. Delete Match

VAR="Hello World"

# Delete first match
echo ${VAR/World}
# Output: Hello 

# Delete all matches
echo ${VAR//World}
# Output: Hello 

# Delete match at beginning
echo ${VAR/#Hello}
# Output:  World

# Delete match at end
echo ${VAR/%World}
# Output: Hello 

Pattern Matching

1. Extract Substring

VAR="/path/to/file.txt"

# Delete shortest prefix match
echo ${VAR##*/}
# Output: file.txt

# Delete longest prefix match (## greedy)
echo ${VAR##*/}
# Output: file.txt

# Delete shortest suffix match
echo ${VAR%.*}
# Output: /path/to/file

# Delete longest suffix match
echo ${VAR%%.*}
# Output: /path/to

2. Practical Applications

# Get filename
FILE="/var/log/nginx/access.log"
echo ${FILE##*/}
# Output: access.log

# Get directory
FILE="/var/log/nginx/access.log"
echo ${FILE%/*}
# Output: /var/log/nginx

# Get extension
FILE="document.pdf"
echo ${FILE##*.}
# Output: pdf

# Remove extension
FILE="document.pdf"
echo ${FILE%.*}
# Output: document

String Comparison

# Check if empty
VAR=""
if [ -z "$VAR" ]; then
    echo "VAR is empty"
fi

# Check if not empty
VAR="hello"
if [ -n "$VAR" ]; then
    echo "VAR is not empty"
fi

# Check equality
VAR1="hello"
VAR2="hello"
if [ "$VAR1" = "$VAR2" ]; then
    echo "equal"
fi

String Case Conversion

VAR="Hello World"

# Convert to uppercase
echo ${VAR^^}
# Output: HELLO WORLD

# Convert to lowercase
echo ${VAR,,}
# Output: hello world

String Concatenation

# Direct concatenation
VAR1="Hello"
VAR2="World"
VAR3="$VAR1 $VAR2"
echo $VAR3
# Output: Hello World

Practical Script Examples

1. Batch Rename Files

# Remove spaces from filenames
for file in *\ *; do
    mv "$file" ${file// /_}
done

2. Extract Domain from URL

URL="https://www.example.com/path/to/page"

# Method 1: Remove protocol and path
DOMAIN=${URL#*://}
DOMAIN=${DOMAIN%%/*}
echo $DOMAIN
# Output: www.example.com

3. Format Date

DATE=$(date +%Y%m%d)
FILENAME="backup_$DATE.tar.gz"
echo $FILENAME
# Output: backup_20260314.tar.gz

4. Check if String Contains Substring

VAR="Hello World"

if [[ "$VAR" == *"World"* ]]; then
    echo "Contains World"
fi

Common Operations Quick Reference

Operation Syntax
Length ${#VAR}
Substring ${VAR:start:len}
Replace ${VAR/old/new}
Remove prefix ${VAR#pattern}
Remove suffix ${VAR%pattern}
Uppercase ${VAR^^}
Lowercase ${VAR,,}

Comments

👍 Was this article helpful?