167

I'm writing a bash script that needs to loop over the arguments passed into the script. However, the first argument shouldn't be looped over, and instead needs to be checked before the loop.

If I didn't have to remove that first element I could just do:

for item in "$@" ; do
  #process item
done

I could modify the loop to check if it's in its first iteration and change the behavior, but that seems way too hackish. There's got to be a simple way to extract the first argument out and then loop over the rest, but I wasn't able to find it.

0

4 Answers 4

187

Use shift.

Read $1 for the first argument before the loop (or $0 if what you're wanting to check is the script name), then use shift, then loop over the remaining $@.

echo "$@";  # will echo all args
shift;  # will remove first arg from the "$@"
echo "$@";  # will echo all args except first one
Sign up to request clarification or add additional context in comments.

6 Comments

See, I knew there was a simple answer. :)
Deserves an upvote, but after Dennis' answers get accepted :)
@mgarciaisaia seems more appropriate to the OP's requirements: remove the first item
this is totally vague- not as helpful as @nos answer below
Would be useful to have an example to avoid having to go to an external link that might disappear one day.
|
171

Another variation uses array slicing:

for item in "${@:2}"
do
    process "$item"
done

This might be useful if, for some reason, you wanted to leave the arguments in place since shift is destructive.

5 Comments

Exactly what I wanted. Now don't need temp variable for dereferencing first argument in "${!1}${@:2}"
@Herms this should be the accepted answer, more readable and not destructive (vs shift)
Technically the currently accepted answer is correct, but this is technically as well as practically correct (since shift is destructive). This is a better answer. So Ideally should be the answer.
For those who wonder, why shift is destructive: It actually removes the element ultimately from the arguments list. Ref: unix.stackexchange.com/a/174568/254204
echo "${a[@]:1}"
62
firstitem=$1
shift;
for item in "$@" ; do
  #process item
done

4 Comments

Remember that $0 is generally the script name.
+1 for showing a simple example; note that the in "$@" part is implied and can be omitted.
@mklement0 You're saying you can just do "for item; do" ?
@DavidDoria: Yes, you can. Try set -- 1 'two' 'three four'; for item; do echo "[$item]"; done
6
q=${@:0:1};[ ${2} ] && set ${@:2} || set ""; echo $q

EDIT

> q=${@:1}
# gives the first element of the special parameter array ${@}; but ${@} is unusual in that it contains (? file name or something ) and you must use an offset of 1;

> [ ${2} ] 
# checks that ${2} exists ; again ${@} offset by 1
    > && 
    # are elements left in        ${@}
      > set ${@:2}
      # sets parameter value to   ${@} offset by 1
    > ||
    #or are not elements left in  ${@}
      > set ""; 
      # sets parameter value to nothing

> echo $q
# contains the popped element

An Example of pop with regular array

   LIST=( one two three )
    ELEMENT=( ${LIST[@]:0:1} );LIST=( "${LIST[@]:1}" ) 
    echo $ELEMENT

5 Comments

Please also explain the code to be more educative.
q=${@:0:1} (btw, you explanation misquotes it as q=${@:1}) should be q=${@:1:1} to be clearer : $@ starts with index 1, presumably to parallel the explicit $1, $2, ... parameters - element 0 has no value (unlike its explicit counterpart, $0, which reflects the shell / script file). [ ${2} ] will break should $2 contain embedded spaces; you won't have that problem if you use [[ ${2} ]] instead. That said, the conditional and the || branch are not needed: if there is only 1 argument, ${@:2} will simply expand to the empty string (cont'd).
(cont'd) You should, however, use set -- so as to ensure that arguments that happen to look like options are not interpreted as such by set and you should double-quote the ${@:2} reference and $q reference in the echo statement. At this point we get: q=${@:1:1}; set -- "${@:2}"; echo "$q". However, q=${@:1:1} is just another (more cumbersome) way of saying $1, and the rest essentially re-implements the shift command; using these features we simply get: q=$1; shift; echo "$q".
@mklement0 I was wonder about where a good place to place a printf to clean the paths ? ( I wish I could be clearer )
Unfortunately, I don't understand. What paths? Clean in what way?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.