che# lib_bash - Bash Utility Library
a small Bash scripting library with system administration utilities, logging mechanisms, and self-updating capabilities.
the functions marked as "OLD" are ridiculouse, don't use them, they will be replaced or deleted, but the new functions are useful.
-
System Maintenance
- Automated package updates
- Dependency management
- Phased update handling
- System cleanup operations
-
Logging System
- Multi-channel logging (persistent/temporary)
- Color-coded output
- Error/warning differentiation
-
Security Features
- Sudo/SSH-ASKPASS integration
- User permission management
- Root access control
-
Advanced Utilities
- Email notifications
- File manipulation tools
- Assertion testing framework
- Self-updating mechanism
- Temporary path management (temp files/dirs) – see docs/README_lib_bash_tempfiles.md
-
Compatibility
- Cross-distribution support
- Error handling with retry logic
- Safe package marking preservation
sudo git clone --depth 1 https://github.com/bitranox/lib_bash.git /usr/local/lib_bash
/usr/local/lib_bash/lib_bash.sh log "Hello World"Set environment variables in your script/shell:
export LIB_BASH_LOGFILE="/var/log/myapp.log"
export LIB_BASH_LOGFILE_ERR="/var/log/myapp_errors.log"
export LIB_BASH_LOG_NO_SYMBOLS=1 # optional: hide emoji/symbol segment in logsIf a variable seems ignored:
- Sudo drops env vars by default. Use one of:
sudo -E LIB_BASH_LOG_NO_SYMBOLS=1 ./your_script …sudo env LIB_BASH_LOG_NO_SYMBOLS=1 ./your_script …- Or allowlist the var in sudoers (
env_keep).
- Different shell/session: export in the same shell that launches the script.
- Value mismatch: some vars are strict. For example:
LIB_BASH_LOG_NO_SYMBOLS:1|true|yes|ondisable symbols (case-insensitive).LIB_BASH_DEBUG_MODE: set toONto enable debug messages.
linux_update # Full system update with cleanupNote: linux_update is implemented in lib_bash_linux_update.sh and is auto-sourced by lib_bash.sh; no extra imports needed.
log "System update started"
log_err "Critical error occurred"
log_warn "Warning: Low disk space"install_package_if_not_present "nginx" "True"
reinstall_keep_marking "openssl"lib_bash_prepend_text_to_file "# Security Settings" "/etc/config"
backup_file "/etc/nginx.conf"# Create and auto-register
f=$(create_temp_file)
d=$(create_temp_dir)
# Registry helpers
list_temppaths
print_temppath_registry
unregister_temppath "$f"
# Clean up everything on script exit
trap 'cleanup_temppaths' EXITSee docs/README_lib_bash_tempfiles.md for full API and examples.
# String output assertions
assert_equal "my_func arg" "expected output"
assert_contains "echo 'foo bar'" "foo"
# Return code assertions
assert_return_code "false" 1
assert_pass "true"
assert_fail "false"See docs/README_lib_assert.md for full API and examples.
The library automatically checks for updates:
# On next execution, updates will be applied
_lib_bash_self_update # Manual update checkTo suppress self-update (e.g., in CI, tests, or restricted environments), set:
export LIB_BASH_DISABLE_SELF_UPDATE=1When this variable is set, lib_bash.sh will skip the self-update logic entirely.
Default locations depend on privilege and script name.
- As user:
${XDG_STATE_HOME:-$HOME/.local/state}/lib_bash/<script>.log - As root:
/var/log/lib_bash/<script>.log
Companion files are created alongside the main log:
- Temporary Log:
<script>_tmp.log - Error Log:
<script>_err.log - Temporary Error Log:
<script>_err_tmp.log
Note on <script> naming:
<script>equals the output ofget_script_stem()fromlib_bash.sh.- It takes the script basename and strips only the last extension (e.g.,
backup.sh→backup). - Dotfiles are preserved unchanged (e.g.,
.envremains.env).
- Coreutils
- Git (for self-updating)
- Mutt (for email functionality)
- SSH-ASKPASS (for GUI password prompts)
GNU General Public License v3.0 - See LICENSE
- Fork the repository
- Create feature branch
- Submit PR with detailed description
# Development setup
git clone [email protected]:your_username/lib_bash.git
cd lib_bash
# Create virtual environment recommended
# Disable self-update during local dev/CI
export LIB_BASH_DISABLE_SELF_UPDATE=1Initial Author: bitranox
Important: Test thoroughly before production use. Some operations require root privileges - ensure proper authorization.