How do you get the nth argument of the previous command on the command line?

If you are in an interactive shell and you enter something like:

echo this is it

Then you can expand the first argument:

echo !^    #=> echo this

Or you can expand the last argument:

echo !$    #=> echo it

But now I'm wondering:

How do I access the nth argument? I looked

+4
source share
1 answer

In this way:

~ $ echo this is it
~ $ echo !!:2
echo is
is

!!:nis n'th arg
!!:n-$is an argument from n'th to the last

Note: !!extends to the last command


According to EDIT ( relocated ):

The second argument to the second command:

~ $ echo foo bar baz # This one is the target
foo bar baz
~ $ echo catz ratz batz
catz ratz batz
~ $ echo !-2:2
echo bar
bar

!-n expands to a command that was 'n' the number of teams before the current command.

: !-1 !! .

+9

All Articles