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_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.

$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> variable to set

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> variable to set

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> variable to set

Argument: $@ arguments to quote

L_quote_bin_printf_v

L_strhash

Convert a string to a number.

Option: -v <var> variable to set

L_strhash_v

L_strhash_bash

Convert a string to a number in pure bash.

Option: -v <var> variable to set

L_strhash_bash_v

L_strstr

Check if string contains substring.

Arguments:

  • $1 string
  • $2 substring

L_strupper

Option: -v <var> variable to set

Argument: $1 String to operate on.

L_strlower

Option: -v <var> variable to set

Argument: $1 String to operate on.

L_capitalize

Capitalize first character of a string.

Option: -v <var> variable to set

Argument: $1 String to operate on

L_uncapitalize

Lowercase first character of a string.

Option: -v <var> variable to set

Argument: $1 String to operate on

L_strip

Remove characters from IFS from begining and end of string

Option: -v <var> variable to set

Arguments:

  • $1 <str> String to operate on.
  • [$2] <str> 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> variable to set

Arguments:

  • $1 <str> String to operate on.
  • [$2] <str> 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> variable to set

Arguments:

  • $1 String to operate on.
  • [$2] <str> 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> variable to set

Argument: $1 prefix

L_list_functions_with_prefix_removed

list functions with prefix and remove the prefix

Option: -v <var> var

Argument: $1 prefix

See: L_list_functions_with_prefix

L_list_functions_with_prefix_removed_v

L_json_escape

Produces a string properly quoted for JSON inclusion

Poor man's jq

Example

   L_json_escape -v tmp "some string"
   echo "{\"key\":$tmp}" | jq .

Option: -v <var> variable to set

See:

L_json_escape_v

L_abbreviation

Choose elements matching prefix.

Option: -v <var> Store the result in the array var.

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 <float> one number
  • $2 <str> operator, one of -lt -le -eq -ne -gt -ge > >= == != <= < <=>
  • $3 <float> second number

L_percent_format

print a string with percent format

A 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"

Option: -v <var> Output variable

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'

Option: -v <var> Output variable

Argument: $1 format string

L_fstring_v

Shellcheck disable= SC2059

L_hexdump

Convert a string to hex dump.

Option: -v <var> Output variable

L_hexdump_v

L_urlencode

Encode a string in percent encoding.

Option: -v <var> Output variable

L_urlencode_v

L_urldecode

Decode percent encoding.

Option: -v <var> Output variable

L_urldecode_v

L_html_escape

Escape characters for html.

Option: -v <var> Output variable

L_html_escape_v

L_str_replace

Replace multiple characters in a string in order.

Note

I think this should be removed.

Example

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

Option: -v <var> Output variable

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_str_replace_v

L_str_count

Count the character in string.

Option: -v <var> Output variable

Arguments:

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

L_str_count_v