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 viaDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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.,\nfor newline,\tfor tab,\xNNfor hexadecimal).
Concatenation
Bash does not use a dedicated concatenation operator. Strings are concatenated via juxtaposition (placing them adjacent to one another).Parameter Expansion (String Operations)
Bash provides built-in string manipulation capabilities through parameter expansion syntax${...}.
Length
Returns the number of characters in the string.Substring Extraction
Extracts a portion of the string starting at a zero-basedoffset 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.
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}
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)
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}
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[[ ... ]])
Master Bash with Deep Grasping Methodology!Learn More





