How do I execute multiple commands using the same argument?
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.
Solution 1:
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
Solution 2:
There is a handy shortcut for a common use case. In your example you are doing:
$ touch file.js
$ openEditor <alt>+<.>
In the second command, the trick is to write openEditor
(with a space after it) followed by Alt+.. This will insert the last argument of the last command, which is file.js
. (If it doesn't work with Alt for some reason, Esc should work as well.)
Since often the "object" is indeed the last argument of the previous command, this can be used frequently. It is easy to remember and will quickly integrate into your set of intuitively used shell shortcuts.
There is a whole bunch of things you can do with this, here's an in-depth article about the possibilities: https://stackoverflow.com/questions/4009412/how-to-use-arguments-from-previous-command.
As a bonus this will work not only in bash but in all programs that use libreadline for processing command line input.
Solution 3:
As far as default interactive shell bash
and scripting shell dash
goes, you can use $_
to recall last argument of the last command.
$ echo "Hello World"; echo same "$_"
Hello World
same Hello World
csh and tcsh have history references, specifically for the last word of the command, you can use !$
, and for individual arguments - !:<index>
:
~% echo foo bar
foo bar
~% echo !$
echo bar
bar
% echo bar baz
bar baz
% echo !:1
echo bar
bar
In general, it's just better to assign whatever is objectA
to a variable, and use it in multiple commands, loops, etc. Alternatively, a function could be a choice:
$ foo(){ echo "$@"; stat --print="%F\n" "$@";}
$ foo testdir
testdir
directory
Solution 4:
I would recommend against the history approach given in steeldriver's answer. This relies on global state, which is always brittle.
Better is to upfront loop over all needed commands, using a proper variabe:
$ for c in touch gedit; do $c foo.txt; done
What's a bit of a problem in general is that Bash doesn't abort when there's a failure, i.e. this actually behaves like touch foo.txt; gedit foo.txt
instead of chaining with &&
. So, to be safe you can add a break
:
$ for c in touch gedit; do $c foo.txt || break; done
Solution 5:
When cooking up a one-liner, I sometimes assign my repeated thing to a shell variable which I use multiple times in the command. I can recall and edit it to use a different arg with up-arrow, control+a, control+right-arrow to get the cursor close to the t=
.
t=testloop; asm-link -d "$t.asm" && perf stat -r2 ./"$t"
Note that this makes it easy to tack on an extension, or a variation on the name.
Also note that I need a ;
after the variable assignment, because var=value cmd
just sets that as an environment variable for that command, and doesn't affect the shell context.