nafigator / bash-helpers
Collections of useful functions for usage in Bash scripts.
Installs: 8 078
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
Language:Shell
This package is auto-updated.
Last update: 2025-05-12 19:07:13 UTC
README
bash-helpers
Collection of useful functions for usage in Bash scripts
Usage
Without installation
#!/usr/bin/env bash source <(curl -s https://raw.githubusercontent.com/nafigator/bash-helpers/1.1.4/src/bash-helpers.sh) inform 'Bash helpers ready!'
Preinstalled
#!/usr/bin/env bash . /usr/local/lib/bash/includes/bash-helpers.sh inform 'Bash helpers ready!'
Installation
- Put bash libs into
/usr/local/lib/bash/includes
dir. - Source
bash-helpers.sh
in executable script:. /usr/local/lib/bash/includes/bash-helpers.sh
Example
[ -d /usr/local/lib/bash/includes ] || sudo mkdir -p /usr/local/lib/bash/includes sudo curl -o /usr/local/lib/bash/includes/bash-helpers.sh https://raw.githubusercontent.com/nafigator/bash-helpers/master/src/bash-helpers.sh sudo chmod +x /usr/local/lib/bash/includes/bash-helpers.sh
Via functions
#!/usr/bin/env bash download_bash_helpers() { printf "Installing bash-helpers\n" [[ ! -d /usr/local/lib/bash/includes ]] || sudo mkdir -p /usr/local/lib/bash/includes sudo curl -so /usr/local/lib/bash/includes/bash-helpers.sh https://raw.githubusercontent.com/nafigator/bash-helpers/master/src/bash-helpers.sh sudo chmod +x /usr/local/lib/bash/includes/bash-helpers.sh return 0 } init_bash_helpers() { [[ -e /usr/local/lib/bash/includes/bash-helpers.sh ]] || download_bash_helpers if [[ ! -x /usr/local/lib/bash/includes/bash-helpers.sh ]]; then printf "Insufficient permissions for bash-helpers execute\n"; return 1 fi . /usr/local/lib/bash/includes/bash-helpers.sh return 0 } init_bash_helpers || exit 1
Via composer
composer require nafigator/bash-helpers
Features:
-
Defines human-readable functions for colors and formatting:
- black()
- red()
- green()
- yellow()
- blue()
- magenta()
- cyan()
- white()
- gray()
- bold()
- clr()
Examples:
printf "$(bold)$(red)ATTENTION$(clr) Save $(cyan)failure$(clr)"
NOTE: For logging purpose colors may be disabled by global
INTERACTIVE
variable:INTERACTIVE=
-
Functions for nicely formatted messages
error
,inform
,warning
.Examples:
inform 'Script start' warning 'Make backup!' error 'File not found'
-
Libs including.
Example:
include google/client || exit 1 include mysql/query-builder || exit 1 include logger; status 'Logger including' $? || exit 1
-
Status messages.
Example:
test -d /usr/local/nonexistent status 'Check /usr/local/nonexistent dir' $? test -d /usr/local/bin status 'Check /usr/local/bin dir' $?
-
Checking dependencies.
Example:
check_dependencies yarn rust || exit 1
-
Debug messages and statuses.
Example:
debug 'This message is hidden' status_dbg 'This status is hidden' $? DEBUG=1 debug 'Visible because of DEBUG variable' test -d /nonexists status_dbg 'Visible because of DEBUG variable' $? test -d /var/log status_dbg 'Visible because of DEBUG variable' $?
Message statuses
[ OK ] - success status
[FAIL] - fail status
[ ?? ] - debug message
[ ++ ] - success debug status
[ -- ] - fail debug status
Versioning
This software follows "Semantic Versioning" specifications. All function signatures declared as public API.
Read more on SemVer.org.