From 32e27e1f84fdd429646c6f86fd75ef0b765b2f01 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Tue, 24 Sep 2019 07:58:03 +0300 Subject: [PATCH] docs: update --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 42ba1f3..0628f5c 100644 --- a/README.md +++ b/README.md @@ -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 } ```