I have these two functions in a bash script. I am just trying to pass arguments directly from one function to another without using global vars, but I can't seem to do it.
function suman {
NODE_EXEC_ARGS= "--inspect";
__handle_global_suman "${NODE_EXEC_ARGS}" "$@"
}
function __handle_global_suman {
# I want $1 to be node exec args and $2 to be args to node script
node $1 ${Z}/cli.js $2;
}
the problem I am having: in the __handle_global_suman function,
the values for $1 and $2 seem to represent the original arguments passed to the suman function, not the arguments passed to __handle_global_suman! I want to be able to access the arguments pass to the __handle_global_suman function.
One solution is to use global variables like the following (but this is bad programming in general):
NODE_EXEC_ARGS=""; // default
ORIGINAL_ARGS=""; // default
function suman {
NODE_EXEC_ARGS="--inspect";
ORIGINAL_ARGS="$@"; // assume this captures the arguments passed to this function, not the original script...
__handle_global_suman
}
# ideally there would be a way to make this function truly private
function __handle_global_suman {
# I want $1 to be node exec args and $2 to be args to node script
node ${NODE_EXEC_ARGS} ${Z}/cli.js ${ORIGINAL_ARGS};
}
hopefully you see what I am trying to do and can help, thanks
ORIGINAL_ARGS="$@"is thus inherently broken -- only an array, not a string, can store that value safely. See shellcheck.net for the more pedestrain quoting errors.functionkeyword is needlessly incompatible with baseline POSIX sh shells (unlike most bash extensions, adding no value over the portable form, which issuman() {with no precedingfunction), and all-uppercase variable names are reserved by POSIX-specified convention for variables with meaning to the operating system or shell.