This commit is contained in:
Augusto Moura 2021-07-22 12:27:47 +02:00 committed by GitHub
commit 2081826242
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -171,8 +171,10 @@ without leading/trailing white-space and with truncated spaces.
**Example Function:** **Example Function:**
```sh ```sh
# Run function as a subshell (note the parenthesis)
# To prevent overriding outside shell options and environment variables
# shellcheck disable=SC2086,SC2048 # shellcheck disable=SC2086,SC2048
trim_all() { trim_all() (
# Usage: trim_all " example string " # Usage: trim_all " example string "
# Disable globbing to make the word-splitting below safe. # Disable globbing to make the word-splitting below safe.
@ -185,10 +187,7 @@ 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.
set +f
}
``` ```
**Example Usage:** **Example Usage:**
@ -269,15 +268,13 @@ This is an alternative to `cut`, `awk` and other tools.
**Example Function:** **Example Function:**
```sh ```sh
split() { # Run function as a subshell (note the parenthesis)
# To prevent overriding outside shell options and environment variables
split() (
# Disable globbing. # Disable globbing.
# This ensures that the word-splitting is safe. # This ensures that the word-splitting is safe.
set -f set -f
# Store the current value of 'IFS' so we
# can restore it later.
old_ifs=$IFS
# Change the field separator to what we're # Change the field separator to what we're
# splitting on. # splitting on.
IFS=$2 IFS=$2
@ -292,13 +289,7 @@ split() {
# Print each list value on its own line. # Print each list value on its own line.
printf '%s\n' "$@" printf '%s\n' "$@"
)
# Restore the value of 'IFS'.
IFS=$old_ifs
# Re-enable globbing.
set +f
}
``` ```
**Example Usage:** **Example Usage:**
@ -323,17 +314,15 @@ $ split "1, 2, 3, 4, 5" ", "
**Example Function:** **Example Function:**
```sh ```sh
trim_quotes() { # Run function as a subshell (note the parenthesis)
# To prevent overriding outside shell options and environment variables
trim_quotes() (
# Usage: trim_quotes "string" # Usage: trim_quotes "string"
# Disable globbing. # Disable globbing.
# This makes the word-splitting below safe. # This makes the word-splitting below safe.
set -f set -f
# Store the current value of 'IFS' so we
# can restore it later.
old_ifs=$IFS
# Set 'IFS' to ["']. # Set 'IFS' to ["'].
IFS=\"\' IFS=\"\'
@ -351,13 +340,7 @@ trim_quotes() {
# Print the quote-less string. # Print the quote-less string.
printf '%s\n' "$*" printf '%s\n' "$*"
)
# Restore the value of 'IFS'.
IFS=$old_ifs
# Re-enable globbing.
set +f
}
``` ```
**Example Usage:** **Example Usage:**