Truncate String

Truncate a string to a specified length with ellipsis.

Code

General
if [ ${#str} -gt $length ]; then echo "${str:0:$((length-3))}..."; else echo "$str"; fi

Parameters

The string to truncate

Maximum length including ellipsis

Server

More Bash Snippets