Accessing previous bash command arguments

Snippet

!! for the previous command

!$ for the last argument of the previous command

!^ for the first argument of the previous command

!* for all the arguments of the previous command, pipes (|) count as an argument

!:n for the nth argument

# the previous command, use !!
$ ls -al foo/*
...
$ !!
ls -al foo/*
 
# last argument of the previous command, use !$
$ mv author_controller.rb  authors_controller.rb
$ git add !$
git add authors_controller.rb 
...
 
# all the arguments of the previous command, use !*
$ ls app/models/author.rb app/models/author.rb.orig
app/models/author.rb       app/models/author.rb.orig
$! ls -al !*
-rw-r--r--  1 kitt  staff  3229 Feb 20 16:18 app/models/author.rb
-rw-r--r--  1 kitt  staff  3229 Feb 20 15:26 app/models/author.rb.orig
 
# get the nth argument of the previous command, use !:n
$ cat /usr/share/dict/words | grep -o -w '\w\{5\}'
...
$ echo !:3
grep

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.