2

I have the following Bash code:

function suman {

    if test "$#" -eq "0"; then
        echo " [suman] using suman-shell instead of suman executable.";
        suman-shell "$@"
    else
        echo "we do something else here"
    fi

}


function suman-shell {

    if [ -z "$LOCAL_SUMAN" ]; then
        local -a node_exec_args=( )
        handle_global_suman node_exec_args "$@"
    else
        NODE_PATH="${NEW_NODE_PATH}" PATH="${NEW_PATH}" node "$LOCAL_SUMAN" --suman-shell "$@";
    fi
}

when the suman command is executed by the user with no arguments, then this is hit:

  echo " [suman] using suman-shell instead of suman executable.";
  suman-shell "$@"

my question is - how can I append an argument to the "$@" value? I need to simply do something like:

handle_global_suman node_exec_args "--suman-shell $@"

obviously that's wrong but I cannot figure out how to do it. What I am not looking for -

handle_global_suman node_exec_args "$@" --suman-shell

the problem is that handle_global_suman works with $1 and $2 and if I make --suman-shell into $3, then I have to change other code, and would rather avoid that.

Preliminary answer:

    local args=("$@")
    args+=("--suman-shell")

    if [ -z "$LOCAL_SUMAN" ]; then
        echo " => No local Suman executable could be found, given the present working directory => $PWD"
        echo " => Warning...attempting to run a globally installed version of Suman..."
        local -a node_exec_args=( )
        handle_global_suman node_exec_args "${args[@]}"
    else
        NODE_PATH="${NEW_NODE_PATH}" PATH="${NEW_PATH}" node "$LOCAL_SUMAN" "${args[@]}";
    fi
  • I think this question could really be pared down and genericized quite a bit, i.e. I think it would be better if there were a more minimal example illustrating the main point of the question. – igal Nov 5 '17 at 19:21
  • 1
    @Ignacio Vazquez-Abrams seems to have answered it, but I will try to clean up the question – Alexander Mills Nov 5 '17 at 19:23
  • Cool. Another small comment: I was actually going to post the same solution as ignacio-vazquez-abrams, but it seemed to me that hauke-laging had also answered it. I think focusing on the subcommand invocation distracted a little bit from the fact that you really wanted the result stored in a variable and not just passed in as an argument. – igal Nov 5 '17 at 19:28
6

Put the arguments into an array and then append to the array.

args=("$@")
args+=(foo)
args+=(bar)
baz "${args[@]}"
1
handle_global_suman node_exec_args --suman-shell "$@"
  • thanks, but look at the last thing I said in the question – Alexander Mills Nov 5 '17 at 18:58
  • I have a variable number of arguments, I'd like to concatenate them all into "$@", if you will, before passing them to another function. – Alexander Mills Nov 5 '17 at 19:01
  • 1
    @AlexanderMills At which position shall --suman-shell be? – Hauke Laging Nov 5 '17 at 19:08
  • the position doesn't matter in this case – Alexander Mills Nov 5 '17 at 19:15
  • I created a "preliminary answer" in the OP, at the very bottom – Alexander Mills Nov 5 '17 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.