Trim whitespace from start and end of string
Trim whitespace from start and end of string

Binary safe

Tests:
test "$(printf '' | ltrim; printf x)" = x || echo fail
test "$(printf 'foo' | ltrim; printf x)" = foox || echo fail
test "$(printf 'foo bar' | ltrim; printf x)" = 'foo barx' || echo fail
test "$(printf ' foo bar' | ltrim; printf x)" = 'foo barx' || echo fail
test "$(printf 'foo bar ' | ltrim; printf x)" = 'foo bar x' || echo fail
test "$(printf ' foo bar ' | ltrim; printf x)" = 'foo bar x' || echo fail
test "$(printf $' \t \tfoo bar \t \t' | ltrim; printf x)" = $'foo bar \t \tx' || echo fail
test "$(printf $' \t\nfoo bar \t\n' | ltrim; printf x)" = $'foo bar \t\nx' || echo fail
test "$(printf $'foo \t\nbar' | ltrim; printf x)" = $'foo \t\nbarx' || echo fail
diff \
    <(printf x; cat ~/.bash_aliases; printf x) \
    <((printf x; cat ~/.bash_aliases; printf x) | ltrim) || echo fail
cmp \
    <(printf x; cat $(which bash); printf x) \
    <((printf x; cat $(which bash); printf x) | ltrim) || echo fail

test "$(printf '' | rtrim; printf x)" = x || echo fail
test "$(printf 'foo' | rtrim; printf x)" = foox || echo fail
test "$(printf 'foo bar' | rtrim; printf x)" = 'foo barx' || echo fail
test "$(printf ' foo bar' | rtrim; printf x)" = ' foo barx' || echo fail
test "$(printf 'foo bar ' | rtrim; printf x)" = 'foo barx' || echo fail
test "$(printf ' foo bar ' | rtrim; printf x)" = ' foo barx' || echo fail
test "$(printf $' \t \tfoo bar \t \t' | rtrim; printf x)" = $' \t \tfoo barx' || echo fail
test "$(printf $' \t\nfoo bar \t\n' | rtrim; printf x)" = $' \t\nfoo barx' || echo fail
test "$(printf $'foo \t\nbar' | rtrim; printf x)" = $'foo \t\nbarx' || echo fail
diff \
    <(printf x; cat ~/.bash_aliases; printf x) \
    <((printf x; cat ~/.bash_aliases; printf x) | rtrim) || echo fail
cmp \
    <(printf x; cat $(which bash); printf x) \
    <((printf x; cat $(which bash); printf x) | rtrim) || echo fail

sukhmel/linux.scripts,sukhmel/linux.scripts