Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grabbing all arguments after nth argument and concatenating them together in bash

So I have a bash script that needs to take an arbitrary number of command line arguments and put them into a single string

Example of what the user would type in:

give <environment> <email> <any number of integers separated by spaces>
give testing [email protected] 1 2 3 4 5

I want to get all of the arguments from $3 to $# and concat them into a string.

My (probably awful) solution right now is

if [ $# -gt 3 ]
then
    env="env="$1
    email="email="$2
    entList=""

    for i in {3..$#}
    do
        if [ $i -eq 3 ]
            then
                    entList=$3
                    shift
            fi;
            if [ $i -gt 3 ]
            then
                    entList=$entList","$3
                   shift
            fi;
     done
fi;

I handle the case of having only three arguments a bit differently, and that one works fine.

Final value of $entList given the example give testing [email protected] 1 2 3 4 5 should be: 1,2,3,4,5

Right now when i run this i get the following Errors:

/usr/local/bin/ngive.sh: line 29: [: {3..5}: integer expression expected
/usr/local/bin/ngive.sh: line 34: [: {3..5}: integer expression expected

Lines 29 and 34 are:

line 29: if [ $i -eq 3 ]
line 34: if [ $i -gt 3 ]

Any help would be appreciated.

like image 727
Snaxib Avatar asked Jun 22 '12 15:06

Snaxib


People also ask

How do I add all arguments in bash?

Assigning the arguments to a regular variable (as in args="$@" ) mashes all the arguments together like "$*" does. If you want to store the arguments in a variable, use an array with args=("$@") (the parentheses make it an array), and then reference them as e.g. "${args[0]}" etc.

How do I pass multiple arguments in bash?

You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the $2 variable, the third argument is referenced by $3 , .. etc.

What does $@ do in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

You're on the right track. Here's my suggestion:

if [ $# -ge 3 ]; then

  env="$1"
  email="$2"
  entlist="$3"

  while shift && [ -n "$3" ]; do
    entlist="${entlist},$3"
  done

  echo "entlist=$entlist"

else

  echo "Arguments: $*"

fi

Note that variables should always be put inside quotes. I'm not sure why you were setting env=env=$1, but I suspect that if you want to recycle that value later, you should do it programatically rather than by evaluating the variable as if it were a statement, in case that was your plan.

like image 169
ghoti Avatar answered Sep 23 '22 20:09

ghoti


Skip first three arguments using a subarray:

all=( ${@} )
IFS=','
threeplus="${all[*]:3}"
like image 35
Fritz G. Mehner Avatar answered Sep 22 '22 20:09

Fritz G. Mehner