trim() {
local s="$*"
# Strip leading whitespace
s="${s#"${s%%[![:space:]]*}"}"
# Strip trailing whitespace
s="${s%"${s##*[![:space:]]}"}"
printf '%s' "$s"
}
trim " hello world " # "hello world"
trim $'\t\n spaces \n\t' # "spaces"
Create a free account and build your private vault. Share publicly whenever you want.