2

I have the following

#!/bin/bash
function f1 ()
{
  echo "${@:1:-2}"
}
f1 1 2 3 4 5 6

I need to echo 1 2 3 4 5 man bash tells me that when I use @ I can't use a negative length.

I resorted to using a calculating ("${@:1:$((${#@}-1))}") which is seeming unorthodox to me.

How do I exclude the last parameter from outputting?

Kusalananda
  • 333,661

1 Answers1

5
echo "${@:1:$#-1}"

The length argument is already in an arithmetic context, so there's no need for $(( ... )), and the number of arguments is given by $# so there's no need to try to use the equivalent of ${#...[@]} on $@.

Kusalananda
  • 333,661