Skip to main content

Overview

The string module provides comprehensive string manipulation utilities in pure Bash. All functions avoid external tools where possible, making them fast and portable. Total functions: 130+

Inspection

Functions for checking string properties and contents.
Returns the length of a string.
string::length "hello world"
# Output: 11
Parameters:
  • str - The string to measure
Returns: Integer length
Check if a string is empty.
if string::is_empty ""; then
  echo "String is empty"
fi
Returns: Exit code 0 if empty, 1 otherwise
Check if a string is non-empty.
if string::is_not_empty "hello"; then
  echo "String has content"
fi
Returns: Exit code 0 if non-empty, 1 otherwise
Check if string contains a substring.
if string::contains "hello world" "world"; then
  echo "Found!"
fi
Parameters:
  • haystack - The string to search in
  • needle - The substring to find
Returns: Exit code 0 if found, 1 otherwise
Check if string starts with a prefix.
string::starts_with "hello world" "hello"
# Returns: 0 (true)
Parameters:
  • str - The string to check
  • prefix - The prefix to match
Check if string ends with a suffix.
string::ends_with "hello.txt" ".txt"
# Returns: 0 (true)
Parameters:
  • str - The string to check
  • suffix - The suffix to match
Check if string matches a regex pattern.
string::matches "hello123" "^[a-z]+[0-9]+$"
# Returns: 0 (true)
Parameters:
  • str - The string to test
  • regex - Bash-compatible regex pattern

Validation functions

Check if string is a valid integer (including negative).
string::is_integer "-42"  # true
string::is_integer "3.14" # false
Check if string is a valid float (supports scientific notation).
string::is_float "3.14"    # true
string::is_float "1.5e-10" # true
Check if string is hexadecimal (with or without 0x prefix).
string::is_hex "0xFF"  # true
string::is_hex "DEADBEEF" # true
Check if string is binary (0b prefix required).
string::is_bin "0b1010" # true
Check if string is octal (leading 0 required).
string::is_octal "0755" # true
Check if string is numeric in any format (int, float, hex, binary, octal).
string::is_numeric "42"     # true
string::is_numeric "0xFF"   # true
string::is_numeric "3.14"   # true
Check if string is alphanumeric only.
string::is_alnum "abc123" # true
string::is_alnum "abc-123" # false
Check if string is alphabetic only.
string::is_alpha "hello" # true
string::is_alpha "hello1" # false

Case conversion

Functions for changing string case.
Convert string to uppercase.
string::upper "hello world"
# Output: HELLO WORLD
Use string::upper::legacy for Bash 3 compatibility (uses tr)
Convert string to lowercase.
string::lower "HELLO WORLD"
# Output: hello world
Use string::lower::legacy for Bash 3 compatibility
Capitalize first character only.
string::capitalise "hello world"
# Output: Hello world
Use string::capitalise::legacy for Bash 3 compatibility
Convert to title case (capitalize each word).
string::title "hello world from bash"
# Output: Hello World From Bash
Requires: awk

Naming convention conversion

Convert between different naming conventions: plain, snake_case, kebab-case, camelCase, PascalCase, CONSTANT_CASE, dot.case, and path/case.

From plain (space-separated)

string::plain_to_snake "hello world"
# Output: hello_world
string::plain_to_kebab "hello world"
# Output: hello-world
string::plain_to_camel "hello world"
# Output: helloWorld
string::plain_to_pascal "hello world"
# Output: HelloWorld
string::plain_to_constant "hello world"
# Output: HELLO_WORLD
string::plain_to_dot "hello world"
# Output: hello.world
string::plain_to_path "hello world"
# Output: hello/world

From snake_case

string::snake_to_plain "hello_world"
# Output: hello world
string::snake_to_kebab "hello_world"
# Output: hello-world
string::snake_to_camel "hello_world"
# Output: helloWorld
string::snake_to_pascal "hello_world"
# Output: HelloWorld
string::snake_to_constant "hello_world"
# Output: HELLO_WORLD
string::snake_to_dot "hello_world"
# Output: hello.world
string::snake_to_path "hello_world"
# Output: hello/world

From kebab-case

Converts hello-worldhello world
Converts hello-worldhello_world
Converts hello-worldhelloWorld
Converts hello-worldHelloWorld
Converts hello-worldHELLO_WORLD
Converts hello-worldhello.world
Converts hello-worldhello/world

From camelCase

Converts helloWorldhello world
Converts helloWorldhello_world
Converts helloWorldhello-world
Converts helloWorldHelloWorld
Converts helloWorldHELLO_WORLD
Converts helloWorldhello.world
Converts helloWorldhello/world

From PascalCase

Converts HelloWorldhello world
Converts HelloWorldhello_world
Converts HelloWorldhello-world
Converts HelloWorldhelloWorld
Converts HelloWorldHELLO_WORLD
Converts HelloWorldhello.world
Converts HelloWorldhello/world

From CONSTANT_CASE

Converts HELLO_WORLDhello world
Converts HELLO_WORLDhello_world
Converts HELLO_WORLDhello-world
Converts HELLO_WORLDhelloWorld
Converts HELLO_WORLDHelloWorld
Converts HELLO_WORLDhello.world
Converts HELLO_WORLDhello/world

From dot.case

Converts hello.worldhello world
Converts hello.worldhello_world
Converts hello.worldhello-world
Converts hello.worldhelloWorld
Converts hello.worldHelloWorld
Converts hello.worldHELLO_WORLD
Converts hello.worldhello/world

From path/case

Converts hello/worldhello world
Converts hello/worldhello_world
Converts hello/worldhello-world
Converts hello/worldhelloWorld
Converts hello/worldHelloWorld
Converts hello/worldHELLO_WORLD
Converts hello/worldhello.world

Trimming and whitespace

Functions for removing or normalizing whitespace.
Remove leading whitespace.
string::trim_left "  hello"
# Output: hello
Remove trailing whitespace.
string::trim_right "hello  "
# Output: hello
Remove both leading and trailing whitespace.
string::trim "  hello world  "
# Output: hello world
Collapse multiple consecutive spaces into one.
string::collapse_spaces "hello    world"
# Output: hello world
Requires: tr
Remove all whitespace from string.
string::strip_spaces "hello world"
# Output: helloworld

Substrings

Functions for extracting parts of strings.
Extract substring starting at position.
string::substr "hello world" 6 5
# Output: world

string::substr "hello world" 6
# Output: world (to end)
Parameters:
  • str - Source string
  • start - Starting position (0-indexed)
  • length - Optional length to extract
Find index of first occurrence of substring.
string::index_of "hello world" "world"
# Output: 6

string::index_of "hello world" "xyz"
# Output: -1 (not found)
Return everything before first occurrence of delimiter.
string::before "[email protected]" "@"
# Output: user
Return everything after first occurrence of delimiter.
string::after "[email protected]" "@"
# Output: example.com
Return everything before last occurrence of delimiter.
string::before_last "path/to/file.txt" "/"
# Output: path/to
Return everything after last occurrence of delimiter.
string::after_last "path/to/file.txt" "/"
# Output: file.txt

Manipulation

Functions for modifying strings.
Replace first occurrence of search string.
string::replace "hello world" "world" "bash"
# Output: hello bash
Replace all occurrences of search string.
string::replace_all "foo bar foo" "foo" "baz"
# Output: baz bar baz
Remove all occurrences of a substring.
string::remove "hello world" "l"
# Output: heo word
Remove first occurrence of a substring.
string::remove_first "hello world" "l"
# Output: helo world
Reverse a string.
string::reverse "hello"
# Output: olleh
Requires: rev (falls back to awk)
Repeat a string n times.
string::repeat "*" 5
# Output: *****
Pad string on the left to reach width.
string::pad_left "42" 5 "0"
# Output: 00042

string::pad_left "hi" 5
# Output:    hi (spaces by default)
Parameters:
  • str - String to pad
  • width - Target width
  • char - Padding character (default: space)
Pad string on the right to reach width.
string::pad_right "hi" 5
# Output: hi   
Center a string within given width.
string::pad_center "hello" 11
# Output:    hello   
Truncate string to max length with ellipsis.
string::truncate "hello world" 8
# Output: hello...

string::truncate "hello world" 5
# Output: he…
Parameters:
  • str - String to truncate
  • max - Maximum length (including suffix)
Behavior:
  • Uses ... for lengths > 5
  • Uses (single ellipsis) for shorter lengths
  • Intelligently handles very small max values

Splitting and joining

Split string by delimiter into lines.
string::split "a,b,c" ","
# Output:
# a
# b
# c
Each element is printed on a separate line.
Join arguments with delimiter.
string::join "," "a" "b" "c"
# Output: a,b,c
Parameters:
  • delimiter - String to insert between elements
  • args... - Elements to join

Encoding and hashing

URL encoding

URL-encode a string (percent-encoding).
string::url_encode "hello world!"
# Output: hello%20world%21
Decode a URL-encoded string.
string::url_decode "hello%20world%21"
# Output: hello world!

Base64 encoding

Encode string as Base64 (uses system base64 command).
string::base64_encode "hello"
# Output: aGVsbG8=
Cross-platform (handles macOS and Linux differences).
Decode Base64 string.
string::base64_decode "aGVsbG8="
# Output: hello
Pure Bash Base64 encoder (no external tools).
string::base64_encode::pure "hello"
Pure Bash Base64 decoder.
string::base64_decode::pure "aGVsbG8="

Base32 encoding

Encode string as Base32.
string::base32_encode "hello"
Requires: base32 or gbase32 (GNU coreutils)
Decode Base32 string.Requires: base32 or gbase32
Pure Bash Base32 encoder (no external tools).
Pure Bash Base32 decoder.

Cryptographic hashes

Compute MD5 hash of string.
string::md5 "hello"
# Output: 5d41402abc4b2a76b9719d911017c592
Requires: md5sum (Linux) or md5 (macOS)
MD5 is not cryptographically secure. Use for checksums only.
Compute SHA-256 hash of string.
string::sha256 "hello"
# Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Requires: sha256sum (Linux) or shasum (macOS)

Generation

Functions for generating random strings and identifiers.
Generate random alphanumeric string.
string::random 16
# Output: a7d9f2k4m8p1q3x5 (example)
Parameters:
  • length - Length of string to generate (default: 16)
Requires: /dev/urandom
Generate a UUID v4 (random).
string::uuid
# Output: 550e8400-e29b-41d4-a716-446655440000 (example)
Requires: uuidgen or /proc/sys/kernel/random/uuid (falls back to manual construction)

Build docs developers (and LLMs) love