Truncate

str_truncate

Truncate a string to fit within a maximum character length. With -e, appends "..." and still fits within the specified length. Pure parameter expansion — no subshells.

Example

str_truncate 10 "Hello, World!"        # => "Hello, Wor"
str_truncate -e 10 "Hello, World!"     # => "Hello, ..."
str_truncate 5 "Hi"                    # => "Hi"

Arguments

  • $1 (string): [-e] Optional flag to append ellipsis for truncated strings
  • $2 (int): Maximum length (or $1 if no flag given)
  • ... (string): Text to truncate

Exit codes

  • 0: Always

Output on stdout

  • Truncated string (no trailing newline)