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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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