6

How can I easily get the second-to-last (penultimate) word/argument from the previous command in a bash interactive shell? I often run commands in the background, and I would like to get the file that was specified before the &, e.g.,

  % echo foo > /tmp/foo &
  % cat !$
  % &

In the example above, !$ gives the last word, &. But I want the second-to-the-last argument, /tmp/foo



Note that it is possible to use word designators with a range like !-1:3, but this is impractical for a command with a large number of words where it's not quickly obvious how many words there are, e.g.,

% (set -x; date; pwd; git status; git diff;  git log | tail -30; date; args=--verbose time make test; date) >& /tmp/log/make.test.20150122-Thu-0834 &
% echo !-1:30
/tmp/log/make.test.20150122-Thu-0834

The example above works, but you have to count and know that the word you want is the 30th word, which is time-consuming and error-prone.

Is there an easy way to get the second-to-last (penultimate) word?

UPDATE: Note that I'm looking for something to type on the command line (e.g., a history expansion, like the !! event designator, or the $ word designator), as opposed to using readline key bindings (e.g., the esc key).

(Note that this question refers to the arguments of a previous command in an interactive shell, and not to arguments passed to a shell script from the command-line, as some answers and comments here are referring to.)

2

2 Answers 2

7

Interactively, you can get the second to last argument of the previous command line with esc - 1 esc . with the default bindings.

More generally, the sequence esc . gets the final token from the previous command line, and you can pass it a numeric argument to specify a different one (for example, esc 1 esc . gets the first argument, and esc 0 esc . gets the command itself).

esc is one of the keybindings for Meta; on many modern keyboards, you can use Alt as Meta as well (you press it at the same time, not as a prefix modifier). I prefer esc as meta because when my muscle memory learned these things, we didn't have no (reliable, consistent) Alt key, and it's still portable all the way to VT100 and Sun keyboards; and at least on my current keyboard (Mac OSX Yosemite) e.g. alt-- does something else than specify a negative numeric argument.

From a previous compound command like this

echo moo; echo bar

the sequence esc 2 esc . gets the semicolon, because that's the second token.

I'm sure there is a way with ! history expansion as well, but I vastly prefer to see what I'm doing. This mechanism brings you the text you want to refer to into your current command line, so you can edit it if you like as well.

4
  • The 1 is optional, too; Esc- defaults to -1.
    – chepner
    Commented Aug 24, 2016 at 2:29
  • @chepner True, esc - esc . gets the second to last one -- thanks, TIL!
    – tripleee
    Commented Aug 24, 2016 at 2:31
  • I've now clarified my question to indicate that I'm looking for a non key-binding solution (e.g., history expansion designators instead). Commented Oct 7, 2020 at 22:10
  • Five years later? Generally we discourage changing the question after you have received answers.
    – tripleee
    Commented Oct 8, 2020 at 4:31
4

Use this to run a command using the 3rd argument of the last command in history:

echo foo > /tmp/foo &
cat !-1:3
1
  • Yes, but it's impractical when the command is large. (see my latest edit to the question with an example of this) Commented Jan 22, 2015 at 22:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.