In my /bin/sh script, I need to pass an indeterminate number of positional parameters, and get the last one (assign the last one to variable $LAST) and then remove this last argument from $@
For example, I call my script with 4 arguments:
./myscript.sh AAA BBB CCC DDD
And inside my script, I need to echo the following:
echo $LAST
DDD
echo $@
AAA BBB CCC
How can I achieve this ?
Use a loop to move all arguments except the last to the end of positional parameters list; so that the last becomes the first, could be referred to by $1, and could be removed from the list using shift.
#!/bin/sh -
count=0
until test $((count+=1)) -ge $#
do
set -- "$@" "$1"
shift
done
LAST=${1-}
shift $((!!$#))
echo "$LAST"
echo "$@"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With