Iterate over arguments in a bash script and make use of their numbers
<p>If I want to iterate over all arguments it is as easy as <code>for i in "$@"; do ...</code>. However, let's say I want to start with the second argument and also make use of the arguments' positions for some basic calculation.</p>

<p>As an example I want to shorten these commands into one loop:</p>

<pre><code>grep -v 'foobar' "$2" | grep -f $file &gt; output1.txt
grep -v 'foobar' "$3" | grep -f $file &gt; output2.txt
grep -v 'foobar' "$4" | grep -f $file &gt; output3.txt
grep -v 'foobar' "$5" | grep -f $file &gt; output4.txt
</code></pre>

<p>I tried many variations like <code>for i in {2..5}; do grep -v 'foobar' "$$i" | grep -f $file &gt; output$(($i-1)).txt; done</code>; however, it seems bash expansion doesn't work like this.</p>

<p>EDIT:</p>

<p>Seems I made a mistake not emphasizing that I need to make use of the argument's position/number (i.e., 2 from <code>$2</code>). It's important because the output files get used separately later in the script. All of the provided answers so far seem correct but I don't know how to use them to make use of the argument's "number".</p>