From 045e24c6e90415dd5136713c6eba0a7affb4a88e Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 12:32:38 +0300 Subject: [PATCH] docs: update --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 47d7fa4..fdc59af 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ align="center">A collection of pure POSIX sh alternatives to external processes. * [STRINGS](#strings) + * [Trim leading and trailing white-space from string](#trim-leading-and-trailing-white-space-from-string) * [Strip pattern from start of string](#strip-pattern-from-start-of-string) * [Strip pattern from end of string](#strip-pattern-from-end-of-string) * [Trim all white-space from string and truncate spaces](#trim-all-white-space-from-string-and-truncate-spaces) @@ -60,6 +61,35 @@ align="center">A collection of pure POSIX sh alternatives to external processes. # STRINGS +## Trim leading and trailing white-space from string + +This is an alternative to `sed`, `awk`, `perl` and other tools. The +function below works by finding all leading and trailing white-space and +removing it from the start and end of the string. + +**Example Function:** + +```sh +trim_string() { + # Usage: trim_string " example string " + trim=${1#${1%%[![:space:]]*}} + trim=${trim%${trim##*[![:space:]]}} + + printf '%s\n' "$trim" +} +``` + +**Example Usage:** + +```shell +$ trim_string " Hello, World " +Hello, World + +$ name=" John Black " +$ trim_string "$name" +John Black +``` + ## Strip pattern from start of string **Example Function:**