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