Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

csh

tcsh

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.

like image 882
Chan Kim Avatar asked Nov 10 '17 07:11

Chan Kim


People also ask

What command brings back the arguments and options from previous line?

In the command-line, you can press alt + . or esc - . It cycles through the last argument of your previous commands.

How do I revert to a previous command in bash?

In Bash and Zsh ctrl + w erases backwards from where the cursor is.

How do you view the previous commands which you have already executed?

2) Up Arrow key Simply works most of the time, using arrow keys is the most popular way to search through previous commands. This is the most known and used a keyboard shortcut to run the previous commands. The usage It's quite straightforward to use the up arrow key to search through the previously executed command.

How do I recall a previous command in Unix?

Using the up and down arrow keys, you can recall previously-entered commands to the command line, edit them and re-issue them. Note that in addition to the left and right arrow keys you can use the Home and End keys to move to the beginning or end of the command line.


1 Answers

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

like image 139
Allan Avatar answered Oct 14 '22 12:10

Allan