mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-22 07:16:35 +02:00
docs: update
This commit is contained in:
parent
397c518833
commit
9f55c1edcd
31
README.md
31
README.md
|
@ -10,6 +10,7 @@ A collection of pure POSIX `sh` alternatives to external processes
|
|||
* [STRINGS](#strings)
|
||||
* [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)
|
||||
|
||||
<!-- vim-markdown-toc -->
|
||||
|
||||
|
@ -51,3 +52,33 @@ rstrip() {
|
|||
$ rstrip "The Quick Brown Fox" " Fox"
|
||||
The Quick Brown
|
||||
```
|
||||
|
||||
## Trim all white-space from string and truncate spaces
|
||||
|
||||
This is an alternative to `sed`, `awk`, `perl` and other tools. The
|
||||
function below works by abusing word splitting to create a new string
|
||||
without leading/trailing white-space and with truncated spaces.
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
# shellcheck disable=SC2086,SC2048
|
||||
trim_all() {
|
||||
# Usage: trim_all " example string "
|
||||
set -f
|
||||
set -- $*
|
||||
printf '%s\n' "$*"
|
||||
set +f
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ trim_all " Hello, World "
|
||||
Hello, World
|
||||
|
||||
$ name=" John Black is my name. "
|
||||
$ trim_all "$name"
|
||||
John Black is my name.
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue