2

I'm wondering how to obtain the last argument passed to a bash function, like this:

#!/bin/bash

function hello() {
    all=$@         # all arguments
    n=$#           # number of arguments
    first_arg=$1   # argument one
    last_arg= ???  # How to get the last argument?
}

How to set $last_arg to the value of the last argument passed to the function?

8
  • @GregHilston Does that excludes the last argument from the second variable?
    – Alan
    Commented Jun 11, 2015 at 12:25
  • Downvote beacuse you posted your whole shell script here. Why are the sed commands and other stuff interesting for us? Your example should look like this: pastebin.com/vyp0drSs Do you was too lazy to make a simple example out of it? I don't understand why somebody should post something like this.
    – hek2mgl
    Commented Jun 11, 2015 at 12:26
  • Hi @hek2mgl, I posted the whole script so you can test its functionality, if needed. I'm trying to follow the rules: stackoverflow.com/help/mcve
    – Alan
    Commented Jun 11, 2015 at 12:28
  • …Complete means Provide all parts needed to reproduce the problem and means not post your whole shell script.
    – hek2mgl
    Commented Jun 11, 2015 at 12:29
  • 1
    I've edited it. Doesn't it look much more clean now?
    – hek2mgl
    Commented Jun 11, 2015 at 12:34

1 Answer 1

2

If all holds the $@, then the last argument is ${all[*]: -1}

Not the answer you're looking for? Browse other questions tagged or ask your own question.