How to 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 the history. But since I need to change the number in two places, I sometimes make a typo. I would like to change the parameter once to change it in both places.

+4
source share
4 answers

In bash:

 !!:gs/9241/9243/ 

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

(zigdon's answer uses the last command starting with pop , for example populate.sh . My answer uses the last command, complete stop. Choose what works for you.)

+10
source

You can also use the replace history feature:

 !pop:gs/9241/1234 

Same:

 $ populate.ksh 9241 && check.ksh 9241 ... $ !pop:gs/9241/1234 populate.ksh 1234 && check.ksh 1234 ... 
+5
source

One solution is to simply create a shell script (populate_check.ksh) that calls scripts in turn:

 r=$1 populate.ksh $r && check.ksh $r 

Or for several parameters:

 for r; do populate.ksh $r && check.ksh $r done 

For tasks that are more transient, you can also parameterize the command to make it easier to edit in history:

 $ r=9241; populate.ksh $r && check.ksh $r 

Or do a few at once:

 $ for r in 9241 9242; do populate.ksh $r && check.ksh $r; done 
+1
source

Correct answers have already been given, but for a more general understanding, read the man page with special attention to "Extending the story" and the associated shell variable (for example, " HISTCONTROL ", " histchars ", etc.). BTW. The pager search function is very useful when reading man bash

+1
source

Source: https://habr.com/ru/post/1277747/


All Articles