Using last parameter within a script?

Hi All!

I cant seem to work out how to use the last parameter passed to a shell script within the script itself.

For example, if I dont know how many parameters will be passed to the script, but want to work on the last one, I know the last parameter will be $x with x being the number stored in $#

So, if I pass 4 parameters from the shell like so:

$myScript param1 param2 param3

And I want to use param three within the script, how can I do this?

Many thanks,

Adam

Try using...

eval last=$"$#"

or

eval echo $"$#"

in general, u can use the $1, $2 as the positional parameter, and if you dont care about the number of parameters then just use the "shift" to pick up one by one parameter. for more on shift, check man shift.

That worked a treat, - thanks! I dont fully understand what 'eval' does but I'll try and read up some more on it.
Thanks again!

I realize this is a long dead post, but I found it useful..

Trouble is, it only works for me for up to 9 files. When a second digit appears it breaks, using the $"$first digit" appended by the second.

I'm using freebsd.

When the index is greater than 9, you must enclose the parameter in braces:

eval "last=\${$#}"