Append argument to arguments list

11,349

Solution 1

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

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

Solution 2

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

Solution 3

handle_global_suman node_exec_args --suman-shell "$@"
Share:
11,349

Related videos on Youtube

Alexander Mills
Author by

Alexander Mills

Updated on September 18, 2022

Comments

  • Alexander Mills
    Alexander Mills almost 2 years

    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
    
    • igal
      igal over 6 years
      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.
    • Alexander Mills
      Alexander Mills over 6 years
      @Ignacio Vazquez-Abrams seems to have answered it, but I will try to clean up the question
    • igal
      igal over 6 years
      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.
  • Alexander Mills
    Alexander Mills over 6 years
    thanks, but look at the last thing I said in the question
  • Alexander Mills
    Alexander Mills over 6 years
    I have a variable number of arguments, I'd like to concatenate them all into "$@", if you will, before passing them to another function.
  • Hauke Laging
    Hauke Laging over 6 years
    @AlexanderMills At which position shall --suman-shell be?
  • Alexander Mills
    Alexander Mills over 6 years
    the position doesn't matter in this case
  • Alexander Mills
    Alexander Mills over 6 years
    I created a "preliminary answer" in the OP, at the very bottom