5月29日 01:19
What are the common string operations in Shell scripts? How to perform string concatenation, slicing, and replacement?
Common string operations in Shell scripts include concatenation, slicing, replacement, comparison, and length calculation.
String Definition
Basic Definition
bash# Define strings str1="Hello World" str2='Hello World' str3=Hello # Multi-line string str4="Line 1 Line 2 Line 3"
String Concatenation
Simple Concatenation
bash# Direct concatenation str1="Hello" str2="World" str3=$str1" "$str2 echo $str3 # Output: Hello World # Use quotes str4="${str1} ${str2}" echo $str4 # Output: Hello World # Multiple string concatenation str5="Hello" " " "World" echo $str5 # Output: Hello World
Dynamic Concatenation
bash# Concatenate variables and text name="John" greeting="Hello, $name!" echo $greeting # Output: Hello, John! # Concatenate command output date_str="Current date: $(date)" echo $date_str # Concatenate array elements arr=("apple" "banana" "cherry") str="${arr[0]}, ${arr[1]}, ${arr[2]}" echo $str # Output: apple, banana, cherry
String Length
Calculate Length
bash# Calculate string length str="Hello World" echo ${#str} # Output: 11 # Use expr expr length "$str" # Output: 11 # Use awk echo "$str" | awk '{print length}'
String Slicing
Basic Slicing
bashstr="Hello World" # Slice from specified position echo ${str:0} # Output: Hello World echo ${str:6} # Output: World # Slice specified length from position echo ${str:0:5} # Output: Hello echo ${str:6:5} # Output: World # Slice from end echo ${str: -5} # Output: World echo ${str: -5:3} # Output: Wor
Delete Substrings
bashstr="Hello World" # Delete shortest matching prefix echo ${str#He} # Output: llo World echo ${str#*o} # Output: World # Delete longest matching prefix echo ${str##*o} # Output: rld # Delete shortest matching suffix echo ${str%ld} # Output: Hello Wor echo ${str%o*} # Output: Hello W # Delete longest matching suffix echo ${str%%o*} # Output: Hell
Extract Filename and Path
bashfilepath="/path/to/file.txt" # Extract filename filename=${filepath##*/} echo $filename # Output: file.txt # Extract directory dirname=${filepath%/*} echo $dirname # Output: /path/to # Extract extension extension=${filepath##*.} echo $extension # Output: txt # Remove extension basename=${filename%.*} echo $basename # Output: file
String Replacement
Basic Replacement
bashstr="Hello World" # Replace first match echo ${str/World/Bash} # Output: Hello Bash # Replace all matches echo ${str//o/O} # Output: HellO WOrld # Delete matches echo ${str/o/} # Output: Hell World echo ${str//o/} # Output: Hell Wrld
Prefix and Suffix Replacement
bashstr="Hello World" # Replace prefix echo ${str/#Hello/Hi} # Output: Hi World # Replace suffix echo ${str/%World/Bash} # Output: Hello Bash
Case Conversion
bashstr="Hello World" # Convert to uppercase echo ${str^^} # Output: HELLO WORLD # Convert to lowercase echo ${str,,} # Output: hello world # Capitalize first letter echo ${str^} # Output: Hello world
String Comparison
Basic Comparison
bashstr1="Hello" str2="World" # Equality comparison if [ "$str1" = "$str2" ]; then echo "Strings are equal" fi # Inequality comparison if [ "$str1" != "$str2" ]; then echo "Strings are not equal" fi # Use [[ ]] if [[ "$str1" == "$str2" ]]; then echo "Strings are equal" fi
Pattern Matching
bashstr="Hello World" # Check if starts with specified string if [[ "$str" == Hello* ]]; then echo "Starts with Hello" fi # Check if ends with specified string if [[ "$str" == *World ]]; then echo "Ends with World" fi # Check if contains specified string if [[ "$str" == *lo* ]]; then echo "Contains 'lo'" fi
Regular Expression Matching
bashstr="Hello123" # Use =~ for regex matching if [[ "$str" =~ ^[A-Za-z]+$ ]]; then echo "Only letters" fi if [[ "$str" =~ ^[A-Za-z0-9]+$ ]]; then echo "Letters and numbers" fi # Extract matched substring if [[ "$str" =~ ([A-Za-z]+)([0-9]+) ]]; then echo "Letters: ${BASH_REMATCH[1]}" echo "Numbers: ${BASH_REMATCH[2]}" fi
String Splitting
Split Using IFS
bashstr="apple,banana,cherry" # Set IFS and split IFS=',' read -ra arr <<< "$str" echo "${arr[0]}" # Output: apple echo "${arr[1]}" # Output: banana echo "${arr[2]}" # Output: cherry # Iterate over split array for item in "${arr[@]}"; do echo "Item: $item" done
Split Using cut
bashstr="apple:banana:cherry" # Split by delimiter echo "$str" | cut -d: -f1 # Output: apple echo "$str" | cut -d: -f2 # Output: banana echo "$str" | cut -d: -f3 # Output: cherry
String Whitespace Removal
Remove Whitespace Characters
bashstr=" Hello World " # Remove leading whitespace str="${str#"${str%%[![:space:]]*}"}" # Remove trailing whitespace str="${str%"${str##*[![:space:]]}"}" echo "$str" # Output: Hello World
Remove Whitespace Using sed
bashstr=" Hello World " # Remove leading whitespace echo "$str" | sed 's/^[[:space:]]*//' # Remove trailing whitespace echo "$str" | sed 's/[[:space:]]*$//' # Remove all whitespace echo "$str" | sed 's/[[:space:]]//g'
Practical Application Examples
Validate Input
bash# Validate email format validate_email() { local email="$1" if [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then echo "Valid email" return 0 else echo "Invalid email" return 1 fi } validate_email "user@example.com"
Format Output
bash# Format string format_string() { local name="$1" local age="$2" local city="$3" printf "Name: %-20s Age: %3d City: %s\n" "$name" "$age" "$city" } format_string "John Doe" 25 "New York"
Batch Rename
bash# Batch change file extensions for file in *.txt; do new_name="${file%.txt}.bak" mv "$file" "$new_name" done
Log Parsing
bash# Parse log line log_line="[2024-01-01 10:00:00] [INFO] User logged in" # Extract time time="${log_line#\[}" time="${time%%\]*}" echo "Time: $time" # Extract log level level="${log_line#*\[}" level="${level%%\]*}" echo "Level: $level" # Extract message message="${log_line#*\] }" echo "Message: $message"
String Operation Best Practices
- Always use quotes: Prevent issues with spaces and special characters
- Use ${} instead of $(): More efficient for string operations
- Check string length: Avoid empty string errors
- Use regex to validate input: Improve data quality
- Use printf for formatted output: More flexible than echo
- Be aware of case sensitivity: Use ^ and , for conversion
- Use arrays to store split results: Easier for subsequent processing