Bashbox is a wannabe bash compiler which aims to help create modular and maintainable bash projects.
To give huge bash codebase a predictable form. Specially for the single script bash projects with thousands of lines of code.
Bashbox compiles your modular bash project into a single file along bringing a standard set of bash enforcements to ensure that your code is safe and less error-prone while you code and test before publishing it in an production environment.
And hey, we finally have some sort of std
library for bash too! Along the ability to create your own library and let others use it. We have a cool stack trace thingy too!
Bashbox design is cargo
inspired but for the bash buddies, so I hope that tells the rest.
You can start developing on Gitpod right away! Just press the button below.
Or simply run the following command to install bashbox in your local system:
curl --proto '=https' --tlsv1.2 -sSfL "https://git.io/Jc9bH" | bash -s selfinstall
Now you are all set for creating awesome bash projects with it:
bashbox new project-name
src/main.sh
use foo;
function main() {
echo "Hello world";
foo "bar";
}
src/foo.sh
function foo() {
local _input="$1";
echo "Hello my name is ${_input}";
}
You can run the project by:
bashbox run --release
If you want to pass some arguments:
bashbox run --release -- arg1 arg2 and-so-on
You can also execute run in build mode:
bashbox build --release --run -- arg1 arg2 and-so-on
Note: Don't pass --run
in build command unless you want to auto-run it after compiling.
A simple example:
bashbox build --release
For more information try bashbox --help
It's simple, just run ./bashbox build --release
after cloning this repository and bashbox will compile itself.
Please note that this project is very experimental and needs more work.
Don't do the followings
- Masking error:
if test "$(some_command --arg)" == "something"; then {
do_something;
} fi
# Here due to the if statement it's impossible in bash to automatically catch the error within "$(some_command --arg)" subshell, which is why we need to assign it separately.
Solution:
local _var_foo;
_var_foo="$(some_command --arg)";
if test "$_var_foo" == "something"; then {
do_something;
} fi
- Undefined default variable in substitution:
local _argv="${1}"; # Can fail # Imagine the user never passed any argument and thus undefined by nature.
local _name_var="${SOME_ENV_VARIABLE}"; # Can fail # You're assuming that some environment variable is available but it might not be and thus undefined.
Solution:
local _argv="${1:-}";
local _name_var="${SOME_ENV_VARIABLE:-}";
# We declare a one-time default value which is empty incase the variable was never defined just to make our program run. Later for safety all you should do is test whether your variable is empty or contains some data.