Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reuse a command in bash with different parameters?

I have two scripts that often need to be run with the same parameter:

$ populate.ksh 9241 && check.ksh 9241

When I need to change the parameter (9241 in this example), I can go back and edit the line in history. But since I need to change the number in two places, I sometimes make a typo. I'd like to be able to change the parameter just once to change it in both places.

like image 235
Jon Ericson Avatar asked Dec 02 '22 09:12

Jon Ericson


2 Answers

In bash:

!!:gs/9241/9243/

Yes, it uses gs///, not s///g. :-)

(zigdon's answer uses the last command starting with pop, such as populate.sh. My answer uses the last command, full stop. Choose which works for you.)

like image 103
Chris Jester-Young Avatar answered Dec 04 '22 21:12

Chris Jester-Young


You can also use the history substitution feature:

!pop:gs/9241/1234

Like so:

$ populate.ksh 9241 && check.ksh 9241
...
$ !pop:gs/9241/1234
populate.ksh 1234 && check.ksh 1234
...
like image 30
zigdon Avatar answered Dec 04 '22 21:12

zigdon