How to Check if String Starts With
• 2 min read
bash
Quick Answer: Check if String Starts With a Prefix
To check if a string starts with a specific prefix in Bash, use pattern matching with [[ "$string" == prefix* ]]. For substring matching, use parameter expansion with ${string:0:${#prefix}}.
Quick Comparison: String Prefix Testing Methods
| Method | Speed | Best For | Readability |
|---|---|---|---|
| [[ == pattern ]] | Very fast | Wildcard patterns | Very clear |
| Parameter expansion | Fastest | Substring prefix | Moderate |
| grep -q ^ | Fast | Piped input | Simple |
| case statement | Very fast | Multiple patterns | Clear |
| expr match | Slower | Legacy scripts | Moderate |
Bottom line: Use pattern matching with [[ ]] for simplicity and clarity.
Checking if String Starts With
Checking string prefixes is essential for validating input, routing logic, and parsing data. Bash provides several clean ways to test if a string starts with specific characters.
Using Wildcard Matching
The simplest approach with pattern matching:
#!/bin/bash
filename="document.txt"
if [[ "$filename" == *.txt ]]; then
echo "Is text file"
fi
Exact Prefix Check
Check for exact prefix string:
#!/bin/bash
text="Hello World"
if [[ "$text" == "Hello"* ]]; then
echo "Starts with 'Hello'"
fi
Practical Examples
#!/bin/bash
# Check if URL starts with https
url="https://example.com"
if [[ "$url" == https://* ]]; then
echo "Secure URL"
fi
# Check if user is root
user="root"
if [[ "$user" == root* ]]; then
echo "Root user"
fi
# Check file type by extension
file="data.json"
if [[ "$file" == *.json ]]; then
echo "JSON file"
fi
Function for Reusable Checks
#!/bin/bash
starts_with() {
local string="$1"
local prefix="$2"
[[ "$string" == "${prefix}"* ]]
}
if starts_with "production" "prod"; then
echo "This is production"
fi
Case Statements for Multiple Prefixes
#!/bin/bash
file="$1"
case "$file" in
*.log)
echo "Log file"
;;
*.sh)
echo "Shell script"
;;
*)
echo "Other file type"
;;
esac
Real-World Example: Route Processing
#!/bin/bash
process_request() {
local path="$1"
if [[ "$path" == "/api/"* ]]; then
echo "API request"
elif [[ "$path" == "/admin/"* ]]; then
echo "Admin request"
elif [[ "$path" == "/static/"* ]]; then
echo "Static file"
else
echo "Regular page"
fi
}
process_request "/api/users"
process_request "/admin/dashboard"
Using Parameter Expansion
Extract everything after prefix:
#!/bin/bash
text="version:1.2.3"
if [[ "$text" == version:* ]]; then
# Get everything after prefix
version="${text#version:}"
echo "Version is: $version"
fi
Performance: Wildcard vs Regex
#!/bin/bash
text="production environment"
# Fast - pattern matching
[[ "$text" == prod* ]] && echo "Yes"
# Also works - regex (slightly slower)
[[ "$text" =~ ^prod ]] && echo "Yes"
Important Notes
- Use
==with[[ ]]for pattern matching - Add
*after the prefix to match anything after - Wildcard matching is faster than regex
- Always quote variables:
[[ "$var" == pattern* ]] - Use
=~with^for regex anchoring
Quick Reference
# Check if starts with
[[ "$string" == "prefix"* ]]
# Function approach
starts_with() { [[ "$1" == "${2}"* ]]; }
# Using regex
[[ "$string" =~ ^prefix ]]
# Multiple checks with case
case "$string" in
prefix*) echo "Match" ;;
esac
Summary
Use [[ "$string" == "prefix"* ]] to check if a string starts with a prefix. This pattern is fast, readable, and doesn’t require external commands.