Substr

str_before

Return the portion of a string before the first occurrence of a delimiter. If the delimiter is not found, the whole string is returned.

Example

str_before "foo:bar:baz" ":"   # => foo
str_before "foobar" ":"        # => foobar

Arguments

  • $1 (string): The string to process
  • $2 (string): The delimiter to search for

Exit codes

  • 0: Always

Output on stdout

  • Substring before the first delimiter

str_after

Return the portion of a string after the first occurrence of a delimiter. If the delimiter is not found, an empty string is returned.

Example

str_after "foo:bar:baz" ":"   # => bar:baz
str_after "foobar" ":"        # => (empty)

Arguments

  • $1 (string): The string to process
  • $2 (string): The delimiter to search for

Exit codes

  • 0: Always

Output on stdout

  • Substring after the first delimiter

str_substr

Extract a substring by zero-based start index and optional length. Wraps bash's ${string:start:length} parameter expansion.

Example

str_substr "hello world" 6       # => world
str_substr "hello world" 0 5     # => hello
str_substr "hello world" -5      # => world

Arguments

  • $1 (string): The string to extract from
  • $2 (int): Zero-based start index (negative counts from end)
  • $3 (int): Optional: number of characters to extract

Exit codes

  • 0: Always

Output on stdout

  • Extracted substring

str_cut

Remove characters from the start or end of a string.

Example

str_cut "foobar" 3         # => bar
str_cut "foobar" 3 end     # => foo
str_cut "foobar" 2 start   # => obar

Arguments

  • $1 (string): The string to process
  • $2 (int): Number of characters to remove
  • $3 (string): Optional: 'start' (default) or 'end'

Exit codes

  • 0: Always
  • 1: Unknown direction argument

Output on stdout

  • String with characters removed

str_get_line

Return the text on a specific line number (1-based). The string may be passed as an argument or piped via stdin.

Example

str_get_line 2 $'line one\nline two\nline three'   # => line two
printf '%s\n' "line one" "line two" | str_get_line 1   # => line one

Arguments

  • $1 (int): Line number (1-based)
  • $2 (string): Optional: string to search (reads stdin if omitted)

Exit codes

  • 0: Always

Output on stdout

  • The text on the specified line

str_get_lines

Return a range of lines from a string (1-based start, line count). The string may be passed as an argument or piped via stdin.

Example

str_get_lines 2 2 $'line one\nline two\nline three'   # => line two\nline three
printf '%s\n' one two three four | str_get_lines 2 2  # => two\nthree

Arguments

  • $1 (int): First line number (1-based)
  • $2 (int): Number of lines to return
  • $3 (string): Optional: string to search (reads stdin if omitted)

Exit codes

  • 0: Always

Output on stdout

  • The specified lines of text