diff --git a/README.md b/README.md index 8032be5..dee9749 100644 --- a/README.md +++ b/README.md @@ -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 } ```