From 09ae87500063f45c559c2ad4a6044e3b5423e436 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 18:22:40 +1000 Subject: [PATCH] Added proper trim function --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b2a591c..eb98dc9 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ scripts and not full blown utilities. * [Strings](#strings) - * [Trim white-space from string.](#trim-white-space-from-string) + * [Trim leading and trailing white-space from string.](#trim-leading-and-trailing-white-space-from-string) + * [Trim all white-space from string and truncate spaces.](#trim-all-white-space-from-string-and-truncate-spaces) * [Split a string on a delimiter.](#split-a-string-on-a-delimiter) * [Change a string to lowercase.](#change-a-string-to-lowercase) * [Change a string to uppercase.](#change-a-string-to-uppercase) @@ -98,9 +99,18 @@ scripts and not full blown utilities. ## Strings -### Trim white-space from string. +### Trim leading and trailing white-space from string. -**NOTE**: This also truncates multiple spaces inside the string. +```sh +trim() { + # Usage: trim " example string " + : "${1#"${1%%[![:space:]]*}"}" + : "${_%"${_##*[![:space:]]}"}" + printf '%s\n' "$_" +} +``` + +### Trim all white-space from string and truncate spaces. ```sh # shellcheck disable=SC2086,SC2048