Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A string in Bash is a contiguous sequence of characters treated as a single unit of data. Because Bash is fundamentally a macro processor and untyped by default, all variables are inherently treated as character strings unless explicitly declared with an integer attribute via declare -i. String manipulation in Bash is primarily handled through parameter expansion rather than dedicated functions or methods.

Quoting Mechanisms

The parsing of a string depends entirely on the quoting mechanism used to define it.
  • Unquoted: Subject to word splitting (based on IFS) and pathname expansion (globbing).
  • Strong Quoting ('...'): Suppresses all interpolation. Every character within single quotes is treated as a literal.
  • Weak Quoting ("..."): Suppresses word splitting and globbing but permits parameter expansion ($), command substitution ($()), and arithmetic expansion ($((...))).
  • ANSI-C Quoting ($'...'): Evaluates backslash-escaped characters according to the ANSI C standard (e.g., \n for newline, \t for tab, \xNN for hexadecimal).
str_unquoted=HelloWorld
str_strong='Literal $var \n'
str_weak="Interpolated $var"
str_ansi=$'Line1\nLine2'

Concatenation

Bash does not use a dedicated concatenation operator. Strings are concatenated via juxtaposition (placing them adjacent to one another).
str1="Hello"
str2="World"
concat_str="${str1} ${str2}!"

Parameter Expansion (String Operations)

Bash provides built-in string manipulation capabilities through parameter expansion syntax ${...}.

Length

Returns the number of characters in the string.
${#string}

Substring Extraction

Extracts a portion of the string starting at a zero-based offset for a specified length. If length is omitted, it extracts to the end of the string. Negative offsets must be separated by a space to avoid conflict with default-value expansions.
${string:offset:length}
${string: -offset:length}

Substring Removal (Stripping)

Removes a substring matching a shell pattern (glob) from either the beginning (prefix) or the end (suffix) of the string.
  • Shortest Prefix: ${string#pattern}
  • Longest Prefix: ${string##pattern}
  • Shortest Suffix: ${string%pattern}
  • Longest Suffix: ${string%%pattern}

# Syntax visualization
${var#*.}   # Removes shortest match of "*." from the front
${var##*.}  # Removes longest match of "*." from the front
${var%.*}   # Removes shortest match of ".*" from the back
${var%%.*}  # Removes longest match of ".*" from the back

Search and Replace

Replaces occurrences of a shell pattern with a replacement string.
  • First Match: ${string/pattern/replacement}
  • All Matches: ${string//pattern/replacement}
  • Prefix Match: ${string/#pattern/replacement} (Pattern must match the beginning)
  • Suffix Match: ${string/%pattern/replacement} (Pattern must match the end)

# Syntax visualization
${var/foo/bar}   # Replaces first 'foo' with 'bar'
${var//foo/bar}  # Replaces all 'foo' with 'bar'

Case Modification (Bash 4.0+)

Modifies the alphabetic case of characters matching an optional pattern (defaults to ?, matching any character).
  • Uppercase First Character: ${string^pattern}
  • Uppercase All Characters: ${string^^pattern}
  • Lowercase First Character: ${string,pattern}
  • Lowercase All Characters: ${string,,pattern}

# Syntax visualization
${var^}   # Capitalizes the first letter
${var^^}  # Converts entirely to uppercase
${var,,}  # Converts entirely to lowercase

String Comparison Operators

String comparison is performed within test commands ([ ... ] or [[ ... ]]). The extended test construct [[ ... ]] is preferred for strings as it prevents word splitting and supports advanced pattern matching.
  • Equality: == or = (True if strings are identical)
  • Inequality: != (True if strings are not identical)
  • Lexicographical Less Than: < (Evaluates based on the current locale’s sorting order)
  • Lexicographical Greater Than: >
  • Zero Length: -z (True if string length is 0)
  • Non-Zero Length: -n (True if string length is greater than 0)
  • Regular Expression Match: =~ (True if the string on the left matches the POSIX Extended Regular Expression on the right; only valid inside [[ ... ]])

# Syntax visualization
[[ "$str1" == "$str2" ]]
[[ "$str1" < "$str2" ]]
[[ -z "$str1" ]]
[[ "$str1" =~ ^[A-Za-z]+[0-9]$ ]]
Master Bash with Deep Grasping Methodology!Learn More