You are not logged in.

#1 2022-01-10 15:34:27

amaro
Member
From: xfce
Registered: 2014-05-09
Posts: 367

how to use last N arguments from previous command

Hello everybody!

I started downloading some music from YT a couple of days ago. I had to stop the process before it's finished.
Now I want to continue the process but I want to use the last 3 arguments of the command.
Got the command from history

hs 140
 2079  y 140 https://www.youtube.com/watch?v=S6jU4NVTnq8 https://www.youtube.com/watch?v=QjudCSZ7TC4 https://www.youtube.com/watch?v=qphQjlPN8TE https://www.youtube.com/watch?v=YvC0hj85BS4 https://www.youtube.com/watch?v=8g6-QvwXXPU https://www.youtube.com/watch?v=0BNdAIS7TBM https://www.youtube.com/watch?v=Wt0iKbF8SW8 https://www.youtube.com/watch?v=abFv4g5qQJw https://www.youtube.com/watch?v=G-0zMnB_z6g PLkaUFxQzXCRhhCHwate0e-cUIt6xeO0gB PLz42GwdiJxL6zSUZ-txc-L6JV-KOKIG5I PLIveWZmYDjBmAdYt7T-NVNx7B3cJutoWs

I googled a bit and found the following instructions for getting different atguments from previous command

!^      first argument
!$      last argument
!*      all arguments
!:2     second argument

!:2-3   second to third arguments
!:2-$   second to last arguments
!:2*    second to last arguments
!:2-    second to next to last arguments

!:0     the command
!!      repeat the previous line

Some of these commands I have used in the past so they are not completely new to me but I still don't know how the get the last N (in this case last 3) arguments.
I also tried 'man bash' but did not find the answer there too.

Any suggestions are appreciated!
Thank you in advance!

p.s. For anyone curious all links are Bruce Springsteen's performances.

Last edited by amaro (2022-01-10 15:36:46)

Offline

#2 2022-02-18 07:52:07

sbmomeni
Member
Registered: 2013-07-26
Posts: 29

Re: how to use last N arguments from previous command

Hello

You can create a bash array out of that command arguments using the set builtin command and then extract the last arguments using bash slicing syntax. For example,

bash$ echo one two three four five six seven
one two three four five six seven
bash$ set -- !!
set -- echo one two three four five six seven
bash$ echo "${@: -3}"
five six seven

Offline

Board footer

Powered by FluxBB