mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-18 05:16:40 +02:00
LICENSE.md | ||
README.md |
pure sh bible
A collection of pure POSIX sh
alternatives to external processes
Table of Contents
STRINGS
Strip pattern from start of string
Example Function:
lstrip() {
# Usage: lstrip "string" "pattern"
printf '%s\n' "${1##$2}"
}
Example Usage:
$ lstrip "The Quick Brown Fox" "The "
Quick Brown Fox
Strip pattern from end of string
Example Function:
rstrip() {
# Usage: rstrip "string" "pattern"
printf '%s\n' "${1%%$2}"
}
Example Usage:
$ 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:
# shellcheck disable=SC2086,SC2048
trim_all() {
# Usage: trim_all " example string "
set -f
set -- $*
printf '%s\n' "$*"
set +f
}
Example Usage:
$ trim_all " Hello, World "
Hello, World
$ name=" John Black is my name. "
$ trim_all "$name"
John Black is my name.