Command Line – Execute Multiple Commands Using the Same Argument

bashcommand line

I wonder if it is possible to make chain actions in Ubuntu terminal like:

action 1 on objectA . then action 2 on objectA 

without having to repeat the name of objectA anymore.

Example:

touch file.js && openEditor "$1" 

or something like that.

Best Answer

With bash History Expansion, you can refer to the nth word of the current command line using !#:n e.g.

$ touch foobar && ls -l !#:1
touch foobar && ls -l foobar
-rw-rw---- 1 steeldriver steeldriver 0 Jun 20 14:54 foobar

$ touch foo bar && ls -l !#:2
touch foo bar && ls -l bar
-rw-rw-r-- 1 steeldriver steeldriver 12 Jun 20 14:58 bar