11

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
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
  • 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 05 '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 05 '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 05 '17 at 19:28

3 Answers3

13

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

args=("$@")
args+=(foo)
args+=(bar)
baz "${args[@]}"
Ignacio Vazquez-Abrams
  • 44,857
  • 7
  • 93
  • 100
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
Elifarley
  • 283
  • 1
  • 2
  • 6
1
handle_global_suman node_exec_args --suman-shell "$@"
Hauke Laging
  • 88,146
  • 18
  • 125
  • 174