creating printf statement using user arguments

I am writing a script in bash and want to perform the operation

I check number of arguments and make a print statement with the passes arguments

If I pass 3 arguments I will do

printf "$frmt" "$1" "$2" "$3"

If I have 4 arguments I do

printf "$frmt" "$1" "$2" "$3" "$4"

etc

Hi,

you can try also with:

echo $*

Ahhh Yes.

In my situation I will be using "$@"

How can I modify this to exclude the last arguments, ot the last two arguments?

---------- Post updated at 08:53 AM ---------- Previous update was at 08:32 AM ----------

I have now coded the following:

length=$(($#-2))
 array="${@:1:$length}"
 echo "All Arguments except last two"
 printf "$frmt" "$array"
 

echo "All Arguments"
 printf "$frmt" "$@"
 

The problem is this:

"$@" allows the each parameter to become a single word, so that "$@" is equivalent to "$1" "$2", because some arguments will have contain embedded blanks.

However calling $array does not allow me to do things as "$@" does, even though the array manages to drop the last two arguments.

---------- Post updated at 09:23 AM ---------- Previous update was at 08:53 AM ----------

I managed to make it work using the following

printf "$frmt" "${@:1:$(($#-2))}"

Hi you could do this:

printf "$frmt" "${@:1:$#-2}"

Assigning to that array "array" would go like this

array=( "${@:1:$length}" )
1 Like

That's very neat :cool: