Rajat Verma
Rajat Verma

Reputation: 2590

How ${@: -1} expand to last argument

I knows the ${para:[start]:[length]} and $@ notation but I'm unable to find out how ${var: -1} evaluates to the last argument.

Upvotes: 3

Views: 51

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799062

From the bash(1) man page:

If parameter is @ ... an offset of -1 evaluates to the last positional parameter.

Upvotes: 3

Andreas Louv
Andreas Louv

Reputation: 47119

Consider it as length - 1 which will resolve in the last character in $var. Same goes for ${var:(-2)}, ...:

var='hello'
printf "%s\n" "${var:(-1)}" # o
printf "%s\n" "${var:(-2)}" # lo
printf "%s\n" "${var:(-3)}" # llo

Upvotes: 5

Related Questions