Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When running commands, sometimes maybe you need to run a command with the argument from the last command. How can you do this?

Of course, excepting the use of the arrow keys and Del keys: ...DelDelDel...Del<new_command>.

share|improve this question

5 Answers 5

up vote 22 down vote accepted

For example, if you run this command:

mkdir long_path_here/new_dir

most likely you'd want to go in the recently created directory. You can do this using next "shortcuts":

  • cd Esc. - type cd and after press Esc followed by . (not in the same time). If the previous command has no arguments, you will get the previous command itself.
  • cd !* - in this case you will get all arguments from the previuos command. If the previous command has no arguments, you will get nothing.
  • cd Alt+. - type cd and after press Alt and . (in the same time). In fact, using this way and continuing to press . (without to release Alt), you will get the last argument for every command from history. If a command has no arguments, you will get the command itself.

In general: <command> Esc. or <command> !* or <command> Alt+..

share|improve this answer

There are a few shortcuts if you want all of the arguments from the previous command, or just the last argument.

  • For all of the arguments: <command> !*
  • For just the last argument: <command> !$

Examples:

ls foo/ bar/
ls !* # Gives the results of ls foo/ bar/

ls foo/ bar/
ls !$ # Gives the results of ls bar/

If you want a single argument from a list of arguments from the previous command, you can use <command> !!:<argNumber>

Example:

ls foo/ bar/ baz/
ls !!:2 # Gives the results of ls bar/
ls foo/ bar/ baz/
ls !!:1 # Gives the results of ls foo/
share|improve this answer
1  
You only need one ! in the single arg version, e.g., !:2 . Assuming default bash. –  belacq Jun 6 '13 at 23:31
    
You can also use !^ in place of !:1. –  Paddy Landau Jun 11 '13 at 15:48

Mentioned !* and !$ is a good thing, but when you need to do a little editing readline shortcuts come to help!

For instance, instead of the countless ...DelDelDel...Del you can just press Ctrl-a(jump to the start of the line) Alt+d(delete to the end of the word)

For more info man readline and search for Default key bindings.

share|improve this answer

To take the nth argument from your previous command, type Alt+n+Alt_. For example, after:

$ echo 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

typing echo followed by Alt2Alt_ will give you this at the next prompt:

$ echo 2

The "screenshot" after you've hit Alt2 is:

(arg: 2) echo

You can repeat Alt_ multiple times to get the nth argument of previous commands in succession.

Another useful idea is to define alias r="fc -s". Then, you can do command substitutions:

$ echo 1
1
$ r echo=history
history 1
27755  history 1
share|improve this answer

something like this !?

ls -la | grep SomeFile

in general there are few ways to do this,

use pipe: | (vertical line)

or

redirects: >>

share|improve this answer
    
-1 arguments from last command, not output from last command... –  Wolfy Jun 7 '13 at 14:33
    
my fault, sorry –  Rodislav Jul 1 '13 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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