This commit is contained in:
Tom Davis 2021-07-22 12:28:25 +02:00 committed by GitHub
commit ade0a749c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -175,8 +175,12 @@ without leading/trailing white-space and with truncated spaces.
trim_all() {
# Usage: trim_all " example string "
# Disable globbing to make the word-splitting below safe.
set -f
# Save and disable the globbing state to make the word-splitting
# below safe.
case $- in
*f*) DIDGLOB=false; ;;
*) DIDGLOB=true; set -f; ;;
esac
# Set the argument list to the word-splitted string.
# This removes all leading/trailing white-space and reduces
@ -186,8 +190,8 @@ trim_all() {
# Print the argument list as a string.
printf '%s\n' "$*"
# Re-enable globbing.
set +f
# Re-set globbing to it's previous condition
$DIDGLOB && set +f
}
```
@ -270,9 +274,12 @@ This is an alternative to `cut`, `awk` and other tools.
```sh
split() {
# Disable globbing.
# This ensures that the word-splitting is safe.
set -f
# Save and disable the globbing state to make the word-splitting
# below safe.
case $- in
*f*) DIDGLOB=false; ;;
*) DIDGLOB=true; set -f; ;;
esac
# Store the current value of 'IFS' so we
# can restore it later.
@ -296,9 +303,8 @@ split() {
# Restore the value of 'IFS'.
IFS=$old_ifs
# Re-enable globbing.
set +f
}
# Re-set globbing to it's previous condition
$DIDGLOB && set +f
```
**Example Usage:**
@ -326,9 +332,12 @@ $ split "1, 2, 3, 4, 5" ", "
trim_quotes() {
# Usage: trim_quotes "string"
# Disable globbing.
# This makes the word-splitting below safe.
set -f
# Save and disable the globbing state to make the word-splitting
# below safe.
case $- in
*f*) DIDGLOB=false; ;;
*) DIDGLOB=true; set -f; ;;
esac
# Store the current value of 'IFS' so we
# can restore it later.
@ -355,8 +364,8 @@ trim_quotes() {
# Restore the value of 'IFS'.
IFS=$old_ifs
# Re-enable globbing.
set +f
# Re-set globbing to it's previous condition
$DIDGLOB && set +f
}
```