A lightweight, terminal‑oriented Bash library for interactive scripts
libnew_shell.sh provides a reusable set of Bash functions that simplify building interactive terminal applications. It handles cursor positioning, coloured output, logging, user input forms, and mirror configuration – all with a clean, minimal API.
- Cursor‑aware input fields –
field:readpositions the prompt and cursor dynamically, wrapping long lines automatically. - Line counting –
count:linecomputes how many terminal lines a string will occupy, perfect for multi‑line prompts. - ANSI colour support – ready‑to‑use functions for 16‑colour (
color:*) and 256‑colour (color256:*) output, with begin/end blocks. - Structured logging –
log:success,log:failure,log:waringprint coloured, formatted messages to stdout or stderr. - Interactive mirror wizard –
tui:mirror:sourcewalks the user through mirror setup, with optional reuse of an existing mirror. - Array membership check –
str:found_matchefficiently tests if a string is present in a space‑separated list.
Simply source the library in your script:
source /path/to/libnew_shell.shOr, if you keep it in a standard location:
source "${HOME}/.local/lib/libnew_shell.sh"# Simple coloured message (green)
color:print "✔ All tests passed" $green
# 256‑colour output to stderr
color256:print "⚠ Warning: disk space low" 208 1 # 1 = stderrx=1; y=1 # initialise coordinates
field:read "Name: " name 2
field:read "Email: " email 2
field:end # reset cursor to (1,1)log:success "setup" "Configuration applied successfully"
log:failure "network" "Could not reach the mirror"
log:waring "disk" "Only 10% free space remaining"tui:mirror:source # interactively set up a mirror sourcefield:read <prompt> <variable_name> [offset]
- Reads user input with
read -e -r -p. - Updates global
x/ycoordinates, wrapping lines as needed. offset(default: 1) adds extra spacing.
field:end
Resets x=1; y=1 and prints a newline to restore normal terminal flow.
count:line <string>
Returns the number of terminal lines the string will occupy, or 0 if the terminal width cannot be determined.
| Function | Description |
|---|---|
color:print <text> <colour_code> [stderr] [no_newline] |
Print coloured text |
color:start <colour_code> / color:end |
Begin/end a colour block |
color256:print <text> <colour_code> [stderr] |
Print 256‑colour text |
color256:start <colour_code> / color256:end |
Begin/end a 256‑colour block |
| Function | Description |
|---|---|
log:success <function_name> <message> |
Log success message |
log:failure <function_name> <message> |
Log failure message |
log:waring <function_name> <message> |
Log warning message |
log:show <function_name> <message> <name_colour> <msg_colour> [stderr] |
Low‑level logging builder |
tui:mirror:source
Interactive mirror configuration. Asks whether to reuse an existing mirror, then collects name, dists, components, and arch. Calls mirror:source and mirror:default (which you must provide in your script).
str:found_match <space_separated_list> <target>
Returns 0 if <target> is found in the list, 1 otherwise. Useful for validating user input against a set of allowed values.
The library uses several global variables – make sure they are not overwritten accidentally:
| Variable | Purpose |
|---|---|
__LIB_TOOLS_LOADED__ |
Guard against multiple sourcing |
red, green, yellow, blue, purple |
Standard ANSI colour codes (31–35) |
pipeline_normal, pipeline_error |
Output stream flags (0 = stdout, 1 = stderr) |
no_new_line |
Flag (1) to suppress trailing newline |
x, y |
Current cursor coordinates (used by field:read) |
- Bash 4.4 or later
tputfromncurses(for terminal width detection)- A terminal that supports ANSI escape codes
This project is licensed under the MIT License – see the LICENSE file for details.
Issues and pull requests are welcome!
Please keep the code POSIX‑compatible where possible, and document any new functions with a brief header comment.
For questions or suggestions, please open an issue on GitHub.
Made with ❤️ for interactive Bash scripting.