views:

32

answers:

2

I normally come here if google doesnt work. So this time it goes like this: In a function, i want to assign to a variable from the 4-th input parameter onward. Example:

function foo {  
  var="$4$5$6..."   
  use var  
  commands using $1, etc  
}

So i think i cannot use shift, since i want to use $1 afterwards. I do not either want to use an extra var to store $1,$2,$3 and shift. So how should it work?

+3  A: 
function foo {
    var=${@:4}
    # ...
}
Alex Howansky
This is bash-specific.
jamessan
That's ok because everybody uses bash, right? :)
Alex Howansky
it works pretty well, thanks.
lukmac
A: 

Someone wanted a version without bashisms? Okay, but you won't like it.

#!/bin/sh

do_something () {
        i=4
        var=
        while [ $i -lt 10 ] ; do
                tmp=
                eval tmp=\"'$'$i\"
                if [ -z "$tmp" ] ; then
                        break
                else
                        var="$var$tmp"
                fi
                i=$(($i+1))
        done
        echo $var
}

do_something one two three four five six "six and a half" seven eight nine

I tested this with dash and FreeBSD's sh, but I cannot really guarantee the portability of evaling things. I also had to limit it to parameters $1 to $9, since after that it stops working.

Sorpigal