19

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.

HoCo_
  • 413

5 Answers5

31

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
steeldriver
  • 136,215
  • 21
  • 243
  • 336
13

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.

Sebastian Stark
  • 6,122
  • 19
  • 48
  • 2
    You can combine this with a digit argument, e.g. to get the second argument hold Alt and press 2. (or Esc, 2, Esc, .), to get the second to last argument press Alt+-, type 2 and press Alt+. (or Esc, -2, Esc, .). – dessert Jun 21 '18 at 13:04
  • @dessert What if I run want more than one of the arguments? E.g. I run echo 1 2 3 4 then I want 2 and 3 for the next command – wjandrea Jun 21 '18 at 21:01
  • @dessert Found it myself! You just need to type something else between them, like a space. To concatenate them, the easiest way is to type a space then backspace. – wjandrea Jun 21 '18 at 21:08
  • 1
    @wjandrea hold Alt and type 2., press spacebar, hold Alt and type 3. – if you want a range, use history expansion: !!:2-3. – dessert Jun 21 '18 at 21:09
9

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
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 1
    I'd just like to point out that $_ works slightly differently than !#:n as the latter is expanded before saving the command in history. So going back in the history would still show the command with $_ while !#:n would have been replaced with its actual value. Both have their pros and cons. – Dan Jun 20 '18 at 20:37
  • 3
    Also, as usual, $_ should be quoted, as otherwise it gets split and globbed like everything else. (We just don't see it here since echo joins its arguments with a single space.) But e.g. touch "hello there"; ls -l $_ won't work. – ilkkachu Jun 21 '18 at 13:51
  • 1
    @Dan I think the biggest pro to $_ is that it's portable. After all, !#:n is bash-specific. – Sergiy Kolodyazhnyy Jun 26 '18 at 03:43
5

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
1

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.

Peter Cordes
  • 2,197