mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-01 13:23:02 +02:00
Merge 2f3c7f86ac
into 26bc7e8d6c
This commit is contained in:
commit
2081826242
41
README.md
41
README.md
|
@ -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:**
|
||||||
|
|
Loading…
Reference in a new issue