mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-09 01:02:30 +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() {
|
trim_all() {
|
||||||
# Usage: trim_all " example string "
|
# 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.
|
# Disable globbing to make the word-splitting below safe.
|
||||||
set -f
|
set -f
|
||||||
|
|
||||||
|
@ -186,8 +190,9 @@ trim_all() {
|
||||||
# Print the argument list as a string.
|
# Print the argument list as a string.
|
||||||
printf '%s\n' "$*"
|
printf '%s\n' "$*"
|
||||||
|
|
||||||
# Re-enable globbing.
|
# Restore the original shell options.
|
||||||
set +f
|
# "${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
|
```sh
|
||||||
split() {
|
split() {
|
||||||
|
# Save the current shell options, so we can
|
||||||
|
# restore them later.
|
||||||
|
options=$-
|
||||||
|
|
||||||
# Disable globbing.
|
# Disable globbing.
|
||||||
# This ensures that the word-splitting is safe.
|
# This ensures that the word-splitting is safe.
|
||||||
set -f
|
set -f
|
||||||
|
@ -296,8 +305,9 @@ split() {
|
||||||
# Restore the value of 'IFS'.
|
# Restore the value of 'IFS'.
|
||||||
IFS=$old_ifs
|
IFS=$old_ifs
|
||||||
|
|
||||||
# Re-enable globbing.
|
# Restore the original shell options.
|
||||||
set +f
|
# "${options##*f*}": Empty if '$options' contains 'f'
|
||||||
|
[ -n "${options##*f*}" ] && set +f
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -326,6 +336,10 @@ $ split "1, 2, 3, 4, 5" ", "
|
||||||
trim_quotes() {
|
trim_quotes() {
|
||||||
# Usage: trim_quotes "string"
|
# Usage: trim_quotes "string"
|
||||||
|
|
||||||
|
# Save the current shell options, so we can
|
||||||
|
# restore them later.
|
||||||
|
options=$-
|
||||||
|
|
||||||
# Disable globbing.
|
# Disable globbing.
|
||||||
# This makes the word-splitting below safe.
|
# This makes the word-splitting below safe.
|
||||||
set -f
|
set -f
|
||||||
|
@ -355,8 +369,9 @@ trim_quotes() {
|
||||||
# Restore the value of 'IFS'.
|
# Restore the value of 'IFS'.
|
||||||
IFS=$old_ifs
|
IFS=$old_ifs
|
||||||
|
|
||||||
# Re-enable globbing.
|
# Restore the original shell options.
|
||||||
set +f
|
# "${options##*f*}": Empty if '$options' contains 'f'
|
||||||
|
[ -n "${options##*f*}" ] && set +f
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue