Chan Kim
Chan Kim

Reputation: 5989

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

I usually use csh (actually my /bin/csh is linked to tcsh, an improved version of csh) and frequently use !$ to refer to the last argument of the last command.
But sometimes I would like to use the last two arguments of the previous command. How can I do that? and I guess I could access the arguments of any previous commands.

I have read How can I recall the argument of the previous bash command? but couldn't find the answer. How can we refer to the second to last argument of the previous command?

For example, If I gave echo tiger rabbit, how can I refer tiger for the following command? An answer for csh would be best but I'm curious about the bash case too.

Upvotes: 7

Views: 4319

Answers (1)

Allan
Allan

Reputation: 12448

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

Upvotes: 16

Related Questions