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

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.


If the previous command had two arguments, like this

ls a.txt b.txt

and you wanted the first one, you could type

!:1

giving

a.txt

Or if you wanted both, you could type

!:1-2

giving

a.txt b.txt

You can extend this to any number of arguments, eg:

!:10-12


!!:n where n is the 0-based position of the argument you want.

For example:

echo 'one' 'two'# "one two"echo !!:2# "two"

The ! prefix is used to access previous commands.

Other useful commands:

  • !$ - last argument from previous command
  • !^ - first argument (after the program/built-in/script) from previous command
  • !! - previous command (often pronounced "bang bang")
  • !n - command number n from history
  • !pattern - most recent command matching pattern
  • !!:s/find/replace - last command, substitute find with replace

More info on command history