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_ipfor getting the IPv4 IP from commandifconfig- Dictionary for Bash 3.x and up.
select_optionto let user to choose option from given array.
match_stringfor testing string that matches give regular expression.regex_stringfor finding the matched pattern and capture groups.replace_stringfor 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_stringfor imploding array to string andexplode_stringfor exploding string to array.load_env_fileto read environment variables file.- Add
escape_string. - Message module for printing colour string.
- Fix skipping the unexpected options for
get_option. - Fix
get_optioncannot handle special characters ‘“$’. - Fix cannot print colour string.
- Add
get_optionto simulate the built-in functiongetopt.