Skip to main content

How to Check if String Ends With

• 2 min read
bash

Quick Answer: Check if String Ends With a Suffix

To check if a string ends with a specific suffix in Bash, use pattern matching with [[ "$string" == *suffix ]]. This is the simplest and most readable approach for checking string suffixes.

Quick Comparison: String Suffix Testing Methods

MethodSpeedBest ForReadability
*[[ == pattern ]]Very fastWildcard patternsVery clear
Parameter expansionFastestSubstring suffixModerate
grep -q $FastPiped inputSimple
case statementVery fastMultiple patternsClear
sed regexFastComplex patternsModerate

Bottom line: Use pattern matching with [[ ]] for simplicity and clarity.


Checking if String Ends With

Checking string suffixes is useful for validating file extensions, determining content types, and routing logic. Bash makes this simple with pattern matching.

Using Wildcard Matching

The cleanest approach:

#!/bin/bash

filename="document.txt"

if [[ "$filename" == *.txt ]]; then
  echo "Text file"
fi

Exact Suffix Check

Match entire end string:

#!/bin/bash

email="user@example.com"

if [[ "$email" == *@example.com ]]; then
  echo "Company email"
fi

Common Examples

#!/bin/bash

# Check file extension
file="photo.jpg"
if [[ "$file" == *.jpg ]] || [[ "$file" == *.png ]]; then
  echo "Image file"
fi

# Check domain
url="https://github.com"
if [[ "$url" == *.com ]]; then
  echo "Commercial domain"
fi

# Check username suffix
username="john_admin"
if [[ "$username" == *_admin ]]; then
  echo "Administrator"
fi

Reusable Function

#!/bin/bash

ends_with() {
  local string="$1"
  local suffix="$2"

  [[ "$string" == *"${suffix}" ]]
}

if ends_with "filename.log" ".log"; then
  echo "Is log file"
fi

Real-World Example: File Type Handler

#!/bin/bash

handle_file() {
  local file="$1"

  if [[ "$file" == *.gz ]]; then
    echo "Compressing: $file"
    gunzip "$file"
  elif [[ "$file" == *.zip ]]; then
    echo "Extracting: $file"
    unzip "$file"
  elif [[ "$file" == *.tar ]]; then
    echo "Extracting tar: $file"
    tar -xf "$file"
  fi
}

handle_file "archive.tar.gz"

Multiple Extensions

#!/bin/bash

is_video() {
  local file="$1"

  case "$file" in
    *.mp4|*.mkv|*.avi|*.mov)
      return 0
      ;;
    *)
      return 1
      ;;
  esac
}

is_video "movie.mp4" && echo "Video file"

Using Parameter Expansion

Extract suffix:

#!/bin/bash

filename="report_2026_02_21.pdf"

if [[ "$filename" == *_*.pdf ]]; then
  # Get everything before extension
  name="${filename%.pdf}"
  echo "Report name: $name"
fi

Regex Alternative

#!/bin/bash

text="user.admin@company.com"

# Check ends with .com using regex
if [[ "$text" =~ \.com$ ]]; then
  echo "Valid .com email"
fi

Important Notes

  • Use == with [[ ]] for pattern matching
  • Add * before the suffix to match anything before
  • Pattern matching is faster than regex
  • Quote variables for safety
  • Use =~ with $ for regex anchoring

Quick Reference

# Check if ends with
[[ "$string" == *"suffix" ]]

# Function
ends_with() { [[ "$1" == *"${2}" ]]; }

# Regex
[[ "$string" =~ suffix$ ]]

# Multiple suffixes
[[ "$string" == *.mp4 || "$string" == *.mkv ]]

Summary

Use [[ "$string" == *"suffix" ]] to check string endings. This is fast, readable, and perfect for file type validation and routing logic.