str
A library of string related functions.
lb_to_lower_all
string
Return a string with all characters converted to lower case.
Parameters:
Name | Type | Description | Default |
string | string | The string to convert to lower case | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_to_upper_all
string
Return a string with all characters converted to upper case.
Parameters:
Name | Type | Description | Default |
string | string | The string to convert to upper case | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_rm_last_char
string
Return a string with the last character removed.
Parameters:
Name | Type | Description | Default |
string | string | The string from which to remove the last character | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_get_last_char
string
Return the last character of a string.
Parameters:
Name | Type | Description | Default |
string | string | The string from which to get the last character | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_length
string
Get the length of a string.
Parameters:
Name | Type | Description | Default |
string | string | The string from which to get the length | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_replace
string
old
new
Return a copy of the string with all occurrences of substring old
replaced by new
.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be worked on | required |
old | string | The substring to be replaced | required |
new | string | The substring to be replaced by | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_reverse_case_all
string
Return a copy of the string with reverse case of all characters.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be converted to reverse case | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_split
string
[delimiter
]
Return an array of the string
components split at the delimiter
.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be split | required |
delimiter | string | The delimiter that is to be used to split string | optional |
Variables:
Name | Type | Description | Default |
LIBBASH_DELIMITER | string | The default delimiter that is to be used to split string | , |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_trim
string
Return a copy of the string with leading and trailing white-space removed.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
Note
This function does not trim the white-space in between words. See lb_trim_all
.
lb_trim_all
string
Return a copy of the string with leading and trailing white-space removed and truncated spaces in between words.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed and truncated | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_trim_quotes
string
Return a copy of the string with single and double quotes removed.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed of quotes | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_strip_all
string
pattern
Return a copy of the string with all copies of a pattern stripped.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed of pattern | required |
pattern | string | The pattern that is to be stripped from string | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_strip
string
pattern
Return a copy of the string with the first copy of a pattern stripped.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed of pattern | required |
pattern | string | The pattern that is to be stripped from string | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_lstrip
string
pattern
Return a copy of the string with a pattern stripped from the start of string.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed of pattern | required |
pattern | string | The pattern that is to be stripped from string | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_rstrip
string
pattern
Return a copy of the string with a pattern stripped from the end of string.
Parameters:
Name | Type | Description | Default |
string | string | The string that is to be trimmed of pattern | required |
pattern | string | The pattern that is to be stripped from string | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_reverse_case
string
Return a copy of the string with reverse case of the first character.
Parameters:
Name | Type | Description | Default |
string | string | The string with which the first character is to be converted to reverse case | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_to_lower
string
Return a string with the first character converted to lower case.
Parameters:
Name | Type | Description | Default |
string | string | The string to convert the first character to lower case | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_to_upper
string
Return a string with the first character converted to upper case.
Parameters:
Name | Type | Description | Default |
string | string | The string to convert the first character to upper case | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
lb_regex
string
pattern
Return a matching regular expression from a pattern
and string
.
Parameters:
Name | Type | Description | Default |
string | string | The string perform the regex on | required |
pattern | string | The pattern used to by regex | required |
Examples:
| #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/str"
function main() {
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper_all "${string}")
printf "lb_to_upper_all: %s\n" "${s}"
s=$(lb_to_lower_all "${string}")
printf "lb_to_lower_all: %s\n" "${s}"
s=$(lb_rm_last_char "${string}")
printf "lb_rm_last_char: %s\n" "${s}"
s=$(lb_get_last_char "${string}")
printf "lb_get_last_char: %s\n" "${s}"
s=$(lb_length "${string}")
printf "lb_length: %s\n" "${s}"
s=$(lb_replace "${string}" "123" "456")
printf "lb_replace: %s\n" "${s}"
s=$(lb_reverse_case_all "${string}")
printf "lb_reverse_case_all: %s\n" "${s}"
printf "LIBBASH_DELIMITER: %s\n" "${LIBBASH_DELIMITER}"
string="apples,oranges,pears,grapes"
printf "string: %s\n" "${string}"
s=$(lb_split "${string}")
printf "lb_split: %s\n" "${s}"
string=" Hello, World "
printf "string: %s\n" "${string}"
s=$(lb_trim "${string}")
printf "lb_trim: %s\n" "${s}"
s=$(lb_trim_all "${string}")
printf "lb_trim_all: %s\n" "${s}"
string="'Hello', \"World\""
printf "string: %s\n" "${string}"
s=$(lb_trim_quotes "${string}")
printf "lb_trim_quotes: %s\n" "${s}"
string="The Quick Brown Fox"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip_all "${string}" "[aeiou]")
printf "lb_strip_all: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "[aeiou]"
s=$(lb_strip "${string}" "[aeiou]")
printf "lb_strip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" "The "
s=$(lb_lstrip "${string}" "The ")
printf "lb_lstrip: %s\n" "${s}"
printf "string: %s\npattern: %s\n" "${string}" " Fox"
s=$(lb_rstrip "${string}" " Fox")
printf "lb_rstrip: %s\n" "${s}"
string="abcABC123ABCabc"
printf "string: %s\n" "${string}"
s=$(lb_to_upper "${string}")
printf "lb_to_upper: %s\n" "${s}"
string="ABCabc123abcABC"
printf "string: %s\n" "${string}"
s=$(lb_to_lower "${string}")
printf "lb_to_lower: %s\n" "${s}"
s=$(lb_reverse_case "${string}")
printf "lb_reverse_case: %s\n" "${s}"
string=" hello"
printf "string: %s\npattern: %s\n" "${string}" '^\s*(.*)'
s=$(lb_regex "${string}" '^\s*(.*)')
printf "lb_regex: %s\n" "${s}"
}
main "${@}"
|
Show source code in str
| #!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"
#------------------------------ Global Variables ------------------------------
LIBBASH_DELIMITER=${LIBBASH_DELIMITER:-","}
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Convert string to lower case
function lb_to_lower_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,,}"
}
# Convert string to upper case
function lb_to_upper_all(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^^}"
}
# Remove the last character
function lb_rm_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1::-1}"
}
# Get the last character
function lb_get_last_char(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1: -1}"
}
# Get the length of a variable
function lb_length(){
lb_check_args_num "${@}" 1
printf '%s\n' "${#1}"
}
# Replace a string
function lb_replace(){
lb_check_args_num "${@}" 3
printf '%s\n' "${1/$2/$3}"
}
function lb_reverse_case_all() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~~}"
}
function lb_split(){
if [[ "${#@}" != 1 ]] && [[ "${#@}" != 2 ]]; then
lb_fail "$(printf "expected 1 or 2 args but got %s\n" "${#@}")"
fi
# Default delimiter
delim=${2:-${LIBBASH_DELIMITER}}
IFS=$'\n' read -d "" -ra arr <<< "${1//$delim/$'\n'}"
printf '%s\n' "${arr[@]}"
}
function lb_trim(){
lb_check_args_num "${@}" 1
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
function lb_trim_all(){
lb_check_args_num "${@}" 1
if [[ "$-" = *f* ]]; then
is_set="false"
else
is_set="true"
fi
set -f
# shellcheck disable=SC2048,SC2086
set -- $*
printf '%s\n' "$*"
if [[ "${is_set}" == "false" ]]; then
set +f
fi
}
function lb_trim_quotes(){
lb_check_args_num "${@}" 1
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
function lb_strip_all(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1//$2}"
}
function lb_strip(){
lb_check_args_num "${@}" 2
printf '%s\n' "${1/$2}"
}
function lb_lstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1##$2}"
}
function lb_rstrip(){
lb_check_args_num "${@}" 2
# shellcheck disable=SC2295
printf '%s\n' "${1%%$2}"
}
function lb_to_upper(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1^}"
}
function lb_to_lower(){
lb_check_args_num "${@}" 1
printf '%s\n' "${1,}"
}
function lb_reverse_case() {
lb_check_args_num "${@}" 1
printf '%s\n' "${1~}"
}
function lb_regex(){
lb_check_args_num "${@}" 2
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
|
Note
bash will use whatever regex engine is installed on the user's system.
Note
This function only prints the first matching group.