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?

0

1 Answer 1

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 $@.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .