12

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
3
  • 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. Commented Nov 5, 2017 at 19:21
  • 1
    @Ignacio Vazquez-Abrams seems to have answered it, but I will try to clean up the question Commented Nov 5, 2017 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. Commented Nov 5, 2017 at 19:28

3 Answers 3

14

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

args=("$@")
args+=(foo)
args+=(bar)
baz "${args[@]}"
3

No need to resort to an array - you can manipulate the arguments themselves by using set --:

$ manipulateArgs() {
  set -- 'my prefix' "$@" 'my suffix'
  for i in "$@"; do echo "$i"; done
}

$ manipulateArgs 'the middle'
my prefix
the middle
my suffix
1
handle_global_suman node_exec_args --suman-shell "$@"
5
  • thanks, but look at the last thing I said in the question Commented Nov 5, 2017 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. Commented Nov 5, 2017 at 19:01
  • 1
    @AlexanderMills At which position shall --suman-shell be? Commented Nov 5, 2017 at 19:08
  • the position doesn't matter in this case Commented Nov 5, 2017 at 19:15
  • I created a "preliminary answer" in the OP, at the very bottom Commented Nov 5, 2017 at 19:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.