A collection of handy functions for the shell script writing, it aims for Bash 3+.
Some features are based on Perl5 to implement (use for PCRE).
All modules are tested in macOS and linux.
Module | File | Description |
---|---|---|
Command | command.sh | Command/Function utility. |
Dictionary | dictionary.sh | Bash dictionary. |
Environment File | env.sh | Environment file reader. |
Message | message.sh | Show color message in shell. |
Network | network.sh | Network utility. |
String | string.sh | String manipulation. |
The following environment variables can be used to configure the behaviour of the library.
Set environment variable LIB_BASH_DEBUG
to true to show error message in stderr
when function get error.
Set environment variable LIB_BASH_ERROR_FG
and LIB_BASH_ERROR_BG
to change the foreground and background color respectively of error message, default is white and red, see ANSI escape code#Colors.
Set environment variable LIB_BASH_INFO_COLOR
to change the color of infomration message, default is green, see ANSI escape code#Colors.
Set environment variable LIB_BASH_QUESTION_COLOR
to change the color of question message, default is cyan, see ANSI escape code#Colors.
All functions may return the following error codes.
Code | Error |
---|---|
65 | Invalid parameters. |
66 | Invalid options. |
67 | No outputs. |
68 | File does not exist. |
69 | Internal error. |
70 | Invalid regular expression. |
71 | Command not found. |
local_ip
for getting the IPv4 IP from commandifconfig
- Dictionary for Bash 3.x and up.
select_option
to let user to choose option from given array.
match_string
for testing string that matches give regular expression.regex_string
for finding the matched pattern and capture groups.replace_string
for replacing string with regular expression.- Fix wrong output for newline character.
- Fix wrong output for trailing newline.
- Fix wrong output for empty string input.
- Fix wrong output for UTF-8.
implode_string
for imploding array to string andexplode_string
for exploding string to array.load_env_file
to read environment variables file.- Add
escape_string
. - Message module for printing colour string.
- Fix skipping the unexpected options for
get_option
. - Fix
get_option
cannot handle special characters ‘“$’. - Fix cannot print colour string.
- Add
get_option
to simulate the built-in functiongetopt
.