14.7 Encoding and decoding strings

The purpose of this module is to let an external process invoke a Bash script with damned command line arguments: strings including blanks or strange characters that may trigger quoting rules. This problem can arise when using scripting languages with some sort of eval command.

The solution is to encode the argument string in hexadecimal or octal format strings, so that all the damned characters are converted to “good” ones. The Bash script can convert them back.

NOTE Internally Bash represents strings as C language style ASCIIZ arrays, so it is impossible to correctly handle null bytes in a Bash variable: the string will get truncated at the null byte.

Function: mbfl_encode_hex ASCII_STRING
Function: mbfl_encode_hex_var _RV ASCII_STRING

Encode a hex string and outputs it on stdout.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

Function: mbfl_decode_hex HEX_STRING
Function: mbfl_decode_hex_var _RV HEX_STRING

Decode a hex string and outputs it on stdout.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

Function: mbfl_decode_oct OCT_STRING
Function: mbfl_decode_oct_var _RV OCT_STRING

Decode a oct string and outputs it on stdout.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

Example:

mbfl_decode_hex 414243
-| ABC

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.