Skip to content

string

string

Collection of functions to manipulate strings.

L_is_true

Return 0 if the string happend to be something like true.

Return 0 when argument is case-insensitive: - true - 1 - yes - y - t - any number except 0 - the character '+'

Argument: $1 str

L_is_false

Return 0 if the string happend to be something like false.

Return 0 when argument is case-insensitive: - false - 0 - no - F - n - the character minus '-'

Argument: $1 str

L_is_true_locale

Return 0 if the string happend to be something like true in locale.

Argument: $1 str

L_is_false_locale

Return 0 if the string happend to be something like false in locale.

Argument: $1 str

L_isprint

Return 0 if all characters in string are printable

Argument: $1 string to check

L_isdigit

Return 0 if all string characters are digits

Argument: $1 string to check

L_is_valid_variable_name

Return 0 if argument could be a variable name.

This function is used to make sure that eval "$1=" will e correct if L_is_valid_variable_name "$1".

Argument: $1 string to check

See: L_is_valid_variable_or_array_element

L_is_valid_variable_or_array_element

Return 0 if argument could be a variable name or array element.

Example

L_is_valid_variable_or_array_element aa           # true
L_is_valid_variable_or_array_element 'arr[elem]'  # true
L_is_valid_variable_or_array_element 'arr[elem'   # false

Argument: $1 string to check

See: L_is_valid_variable_name

L_is_valid_function_name

Is the string a valid Bash opinionated function name?

Almost anything is valid Bash function name.

Argument: $1 string to check

See:

L_is_integer

Return 0 if the string characters is an integer

Argument: $1 string to check

L_is_float

Return 0 if the string characters is a float

Argument: $1 string to check

$L_NL

newline

$L_TAB

tab

$L_SOH

Start of heading

$L_STX

Start of text

$L_EOT

End of Text

$L_EOF

End of transmission

$L_ENQ

Enquiry

$L_ACK

Acknowledge

$L_BEL

Bell

$L_BS

Backspace

$L_HT

Horizontal Tab

$L_LF

Line Feed

$L_VT

Vertical Tab

$L_FF

Form Feed

$L_CR

Carriage Return

$L_SO

Shift Out

$L_SI

Shift In

$L_DLE

Data Link Escape

$L_DC1

Device Control 1

$L_DC2

Device Control 2

$L_DC3

Device Control 3

$L_DC4

Device Control 4

$L_NAK

Negative Acknowledge

$L_SYN

Synchronous Idle

$L_ETB

End of Transmission Block

$L_CAN

Cancel

$L_EM

End of Medium

$L_SUB

Substitute

$L_ESC

Escape

$L_FS

File Separator

$L_GS

Group Separator

$L_RS

Record Separator

$L_US

Unit Separator

$L_DEL

Delete

$L_LBRACE

Left brace character

$L_RBRACE

Right brace character

$L_UUID

Looks random.

See: L_uuid4

$L_ALLCHARS

255 bytes with all possible 255 values

$L_ASCII_LOWERCASE

All lowercase characters a-z

$L_ASCII_UPPERCASE

All uppercase characters A-Z

$L_GPL_LICENSE_NOTICE_3_OR_LATER

The GPL3 or later License notice.

See: https://www.gnu.org/licenses/gpl-howto.en.html#license-notices

$L_FREE_SOFTWARE_NOTICE

notice that the software is a free software.

L_quote_setx

Output a string with the same quotating style as does bash in set -x

Option: -v <var> Store the output in variable instead of printing it.

Argument: $@ arguments to quote

L_quote_setx_v

L_quote_printf

Output a string with the same quotating style as does bash with printf

For single argument, just use printf -v var "%q" "$var". Use this for more arguments, like printf -v var "%q " "$@" results in a trailing space.

Option: -v <var> Store the output in variable instead of printing it.

Argument: $@ arguments to quote

L_quote_printf_v

L_quote_bin_printf

Output a string with the same quotating style as does /bin/printf

Option: -v <var> Store the output in variable instead of printing it.

Argument: $@ arguments to quote

L_quote_bin_printf_v

L_strhash

Convert a string to a number.

Option: -v <var> Store the output in variable instead of printing it.

L_strhash_v

L_strhash_bash

Convert a string to a number in pure bash.

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 string

L_strhash_bash_v

L_strstr

Check if string contains substring.

Arguments:

  • $1 string
  • $2 substring

L_strupper

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 String to operate on.

L_strlower

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 String to operate on.

L_capitalize

Capitalize first character of a string.

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 String to operate on

L_uncapitalize

Lowercase first character of a string.

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 String to operate on

L_strip

Remove characters from IFS from begining and end of string

Option: -v <var> Store the output in variable instead of printing it.

Arguments:

  • $1 String to operate on.
  • [$2] Optional glob to strip, default is [:space:]

L_strip_v

Shellcheck disable= SC2295

L_lstrip

Remove characters from IFS from begining of string

Option: -v <var> Store the output in variable instead of printing it.

Arguments:

  • $1 String to operate on.
  • [$2] Optional glob to strip, default is [:space:]

L_lstrip_v

Shellcheck disable= SC2295

L_rstrip

Remove characters from IFS from begining of string

Option: -v <var> Store the output in variable instead of printing it.

Arguments:

  • $1 String to operate on.
  • [$2] Optional glob to strip, default is [:space:]

L_rstrip_v

Shellcheck disable= SC2295

L_list_functions_with_prefix

list functions with prefix

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 prefix

L_list_functions_with_prefix_v

L_list_functions_with_prefix_removed

list functions with prefix and remove the prefix

Option: -v <var> Store the output in variable instead of printing it.

Argument: $1 prefix

See: L_list_functions_with_prefix

L_list_functions_with_prefix_removed_v

L_abbreviation

Choose elements matching prefix.

Option: -v <var> Store the output in variable instead of printing it.

Arguments:

  • $1 prefix
  • $@ elements

L_abbreviation_v

L_float_cmp

compare two float numbers

The '<=>' operator returns 9 when $1 < $2, 10 when $1 == $2 and 11 when $1 > $2.

Example

L_float_cmp 123.234 -le 234.345
echo $?  # outputs 0
L_exit_to ret L_float_cmp 123.234 -le 234.345
echo "$ret"  # outputs 0

Arguments:

  • $1 one number
  • $2 operator, one of -lt -le -eq -ne -gt -ge > >= == != <= < <=>
  • $3 second number

L_float

A simple wrapper script around awk to evaluate float expressions.

Argument: $1 Expression to evaluate.

L_percent_format

Print a string with percent format.

Simple implementation of percent formatting in bash using regex and printf.

Example

name=John
declare -A age=([John]=42)
L_percent_format "Hello, %(name)s! You are %(age[John])10s years old.\n"

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

Arguments:

  • $1 format string
  • $@ arguments

L_percent_format_v

Shellcheck disable= SC2059

L_fstring

print a string with f-string format

A simple implementation of f-strings in bash using regex and printf.

Example

name=John
declare -A age=([John]=42)
L_fstring 'Hello, {name}! You are {age[John]:10s} years old.\n'

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

Argument: $1 format string

L_fstring_v

Shellcheck disable= SC2059

L_hexdump

Convert a string to hex dump.

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

L_hexdump_v

L_urlencode

Encode a string in percent encoding.

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

L_urlencode_v

L_urldecode

Decode percent encoding.

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

L_urldecode_v

L_html_escape

Escape characters for html.

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

L_html_escape_v

L_string_replace

Replace multiple characters in a string in order.

Note

I think this should be removed.

Example

L_string_replace -v string "$string" "&" "&amp;" "<" "&lt;"

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

Arguments:

  • $1 String to operate on.
  • $2 String to replace.
  • $3 Replacement.
  • $@ String to replace and replacement can be repeated multiple times.

See: L_html_escape

L_string_replace_v

L_string_count

Count the character in string.

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

Arguments:

  • $1 String.
  • $2 Character to count in string.

L_string_count_v

L_string_count_lines

Count lines in a string.

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -h Print this help and return 0.

Arguments:

  • $1 String.
  • [$2] Line characters. Default: newline.

L_string_count_lines_v

L_string_unquote

Split a string with quotes without the risk of execution anything.

Rules: - https://www.gnu.org/software/bash/manual/html_node/Escape-Character.html - https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html - https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html - https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html

Why not xargs? To support ANSI-C quoting style $'' and support newlines in quotes.

Why not declare? Declare allows execution, declare -a array='($(echo something >&2))'executes echo.

Example

$ L_string_unquote -v cmd "ls -l 'somefile; rm -rf ~'"
$ declare -p cmd
declare -a cmd=([0]="ls" [1]="-l" [2]="somefile; rm -rf ~")

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -c Enable ignoring comments.
  • -A Disable ANSI-C quoting.
  • -h Print this help and return 0.
  • -q Without -v, instead of printing one word per line, output words quoted with printf %q.

Shellcheck disable= SC1003