aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2019-09-24 07:58:03 +0300
committerDylan Araps <dylan.araps@gmail.com>2019-09-24 07:58:03 +0300
commit32e27e1f84fdd429646c6f86fd75ef0b765b2f01 (patch)
tree998a0d44936127bdc82fba2048dbf4c30098bb3a
parent4011d6482b668433eb6edd7f5993f650703e059d (diff)
downloadpure-sh-bible-32e27e1f84fdd429646c6f86fd75ef0b765b2f01.tar.gz
pure-sh-bible-32e27e1f84fdd429646c6f86fd75ef0b765b2f01.zip
docs: update
-rw-r--r--README.md18
1 files changed, 18 insertions, 0 deletions
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
}
```