Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last word in bash variable

Tags:

bash

I have something like that:

...
args=$*
echo $args
...

result is

unusable1 unusable2 useful useful ... useful unusable3

I need remove all "unusable" args. They always at first, second and last position. After some investigation i find ${*:3} bash syntax. It help remove first two.

...
args=${*:3}
echo $args
...

result is

useful useful ... useful unusable3

But I can't find how to remove last word using same nice syntax.

like image 691
Stepan Loginov Avatar asked Dec 14 '22 22:12

Stepan Loginov


1 Answers

You can use a function/script like this to print all but last arguments:

func() {
   echo "${@:1:$#-1}";
}

func aa bb cc dd ee
aa bb cc dd

func foo bar baz hello how are you
foo bar baz hello how are
like image 98
anubhava Avatar answered Jan 03 '23 00:01

anubhava