docs: update

This commit is contained in:
Dylan Araps 2019-09-24 07:58:03 +03:00
parent 4011d6482b
commit 32e27e1f84

View file

@ -139,7 +139,15 @@ removing it from the start and end of the string.
```sh
trim_string() {
# Usage: trim_string " example string "
# Remove all leading white-space.
# '${1%%[![:space:]]*}': Strip everything but leading white-space.
# '${1#${XXX}}': Remove the white-space from the start of the string.
trim=${1#${1%%[![:space:]]*}}
# Remove all trailing white-space.
# '${trim##*[![:space:]]}': Strip everything but trailing white-space.
# '${trim#${XXX}}': Remove the white-space from the end of the string.
trim=${trim%${trim##*[![:space:]]}}
printf '%s\n' "$trim"
@ -169,9 +177,19 @@ without leading/trailing white-space and with truncated spaces.
# shellcheck disable=SC2086,SC2048
trim_all() {
# Usage: trim_all " example string "
# Disable globbing to make the word-splitting below safe.
set -f
# Set the argument list to the word-splitted string.
# This removes all leading/trailing white-space and reduces
# all instances of multiple spaces to a single (" " -> " ").
set -- $*
# Print the argument list as a string.
printf '%s\n' "$*"
# Re-enable globbing.
set +f
}
```