mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-17 21:06:36 +02:00
Reduce side effects of string functions
This commit is contained in:
parent
d744927f52
commit
e3fc05536d
27
README.md
27
README.md
|
@ -175,6 +175,10 @@ without leading/trailing white-space and with truncated spaces.
|
|||
trim_all() {
|
||||
# Usage: trim_all " example string "
|
||||
|
||||
# Save the current shell options, so we can
|
||||
# restore them later.
|
||||
options=$-
|
||||
|
||||
# Disable globbing to make the word-splitting below safe.
|
||||
set -f
|
||||
|
||||
|
@ -186,8 +190,9 @@ trim_all() {
|
|||
# Print the argument list as a string.
|
||||
printf '%s\n' "$*"
|
||||
|
||||
# Re-enable globbing.
|
||||
set +f
|
||||
# Restore the original shell options.
|
||||
# "${options##*f*}": Empty if '$options' contains 'f'
|
||||
[ -n "${options##*f*}" ] && set +f
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -270,6 +275,10 @@ This is an alternative to `cut`, `awk` and other tools.
|
|||
|
||||
```sh
|
||||
split() {
|
||||
# Save the current shell options, so we can
|
||||
# restore them later.
|
||||
options=$-
|
||||
|
||||
# Disable globbing.
|
||||
# This ensures that the word-splitting is safe.
|
||||
set -f
|
||||
|
@ -296,8 +305,9 @@ split() {
|
|||
# Restore the value of 'IFS'.
|
||||
IFS=$old_ifs
|
||||
|
||||
# Re-enable globbing.
|
||||
set +f
|
||||
# Restore the original shell options.
|
||||
# "${options##*f*}": Empty if '$options' contains 'f'
|
||||
[ -n "${options##*f*}" ] && set +f
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -326,6 +336,10 @@ $ split "1, 2, 3, 4, 5" ", "
|
|||
trim_quotes() {
|
||||
# Usage: trim_quotes "string"
|
||||
|
||||
# Save the current shell options, so we can
|
||||
# restore them later.
|
||||
options=$-
|
||||
|
||||
# Disable globbing.
|
||||
# This makes the word-splitting below safe.
|
||||
set -f
|
||||
|
@ -355,8 +369,9 @@ trim_quotes() {
|
|||
# Restore the value of 'IFS'.
|
||||
IFS=$old_ifs
|
||||
|
||||
# Re-enable globbing.
|
||||
set +f
|
||||
# Restore the original shell options.
|
||||
# "${options##*f*}": Empty if '$options' contains 'f'
|
||||
[ -n "${options##*f*}" ] && set +f
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue