How to Recall the Argument of the Previous Bash Command

How can I recall the argument of the previous bash command?

You can use $_ or !$ to recall the last argument of the previous command.

Also Alt + . can be used to recall the last argument of any of the previous commands.

How to use arguments from previous command?

Just as M-. (meta-dot or esc-dot or alt-dot) is the readline function yank-last-arg, M-C-y (meta-control-y or esc-ctrl-y or ctrl-alt-y) is the readline function yank-nth-arg. Without specifying n, it yanks the first argument of the previous command.

To specify an argument, press Escape and a number or hold Alt and press a number. You can do Alt--to begin specifying a negative number then release Alt and press the digit (this will count from the end of the list of arguments.

Example:

Enter the following command

$ echo a b c d e f g
a b c d e f g

Now at the next prompt, type echo (with a following space), then

Press Alt-Ctrl-y and you'll now see:

$ echo a

without pressing Enter yet, do the following

Press Alt-3 Alt-Ctrl-y

Press Alt-- 2 Alt-Ctrl-y

Now you will see:

$ echo ace

By the way, you could have put the echo on the line by selecting argument 0:

Press Alt-0 Alt-Ctrl-y

Edit:

To answer the question you added to your original:

You can press Alt-0 then repeatedly press Alt-. to step through the previous commands (arg 0). Similarly Alt-- then repeating Alt-. would allow you to step through the previous next-to-last arguments.

If there is no appropriate argument on a particular line in history, the bell will be rung.

If there is a particular combination you use frequently, you can define a macro so one keystroke will perform it. This example will recall the second argument from previous commands by pressing Alt-Shift-Y. You could choose any available keystroke you prefer instead of this one. You can press it repeatedly to step through previous ones.

To try it out, enter the macro at a Bash prompt:

bind '"\eY": "\e2\e."'

To make it persistent, add this line to your ~/.inputrc file:

"\eY": "\e2\e."

Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers.

how can I recall previous commands' arguments? (in csh, and in bash?)

Using history expansion you can pick a specific command from the
history, execute it as it is, or modify it and execute it based on your
needs. The ! starts the history expansion.

  • !! Repeats the previous command
  • !10 Repeat the 10th command from the history
  • !-2 Repeat the 2nd command (from the last) from the history
  • !string Repeat the command that starts with “string” from the history
  • !?string Repeat the command that contains the word “string” from the history
  • ^str1^str2^ Substitute str1 in the previous command with str2 and execute it
  • !!:$ Gets the last argument from the previous command.
  • !string:n Gets the nth argument from the command that starts with “string” from the history.

  • !^ first argument of the previous command

  • !$ last argument of the previous command
  • !* all arguments of the previous command
  • !:2 second argument of the previous command
  • !:2-3 second to third arguments of the previous command
  • !:2-$ second to last arguments of the previous command
  • !:2* second to last arguments of the previous command
  • !:2- second to next to last arguments of the previous command
  • !:0 the command itself

Last but not least, I would also recommend you to press on Alt + . to access to the last argument of any of the previous commands you have entered

Recall all arguments from a previously executed command in bash shell

Try leaving out the second !:

$ ls foo bar baz
foo bar baz
$ echo !ls:*
echo foo bar baz
foo bar baz

Bash refer to previous arguments in same line


mkdir tmp && cd "$_"

is what you're looking for, I believe (you can drop the double quotes if you're sure that quoting is not needed).

$_ expands differently in different contexts, but the one that matters here (from man bash , v3.2.51):

expands to the last argument to the previous command, after expansion.

Background:

Note: the following discusses bash, but it seems to apply to zsh in principle as well.

$_ is a so-called special parameter, which you'll find listed among others (without the $ prefix) in the Special Parameters section of man bash:

  • Works both in interactive shells and in scripts.
  • Works at the individual command level, not at the line level.

By contrast, !-prefixed tokens (such as !$ to recall the most recent line's last token) relate to the shell's [command] history expansion (section HISTORY EXPANSION of man bash):

  • By default they work only in interactive shells, but that can be changed with set -o history and set -o histexpand (thanks, @chepner)).
  • Work at the line level (more accurately: everything that was submitted with one Enter keystroke, no matter how many individual commands or lines it comprised).


Related Topics



Leave a reply



Submit