Linux 我如何回忆上一个 bash 命令的参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3371294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:19:39  来源:igfitidea点击:

How can I recall the argument of the previous bash command?

linuxbashunixcommand

提问by The Coder

Is there a way in Bash to recall the argument of the previous command?

Bash 有没有办法回忆上一个命令的参数?

I usually do vi file.cfollowed by gcc file.c.

我通常这样做,vi file.c其次是gcc file.c.

Is there a way in Bash to recall the argument of the previous command?

Bash 有没有办法回忆上一个命令的参数?

采纳答案by codaddict

You can use $_or !$to recall the last argument of the previous command.

您可以使用$_!$来调用上一个命令的最后一个参数。

Also Alt + .can be used to recall the last argument of any of the previous commands.

Alt + .可用于调用任何先前命令的最后一个参数。

回答by Justin Ethier

Yes, you can use !$to recall the last argument of the preceding command.

是的,您可以使用!$来调用前面命令的最后一个参数。

回答by Robert Gowland

If the previous command had two arguments, like this

如果前面的命令有两个参数,像这样

ls a.txt b.txt

and you wanted the first one, you could type

你想要第一个,你可以输入

!:1

giving

给予

a.txt

Or if you wanted both, you could type

或者,如果你同时想要,你可以输入

!:1-2

giving

给予

a.txt b.txt

You can extend this to any number of arguments, eg:

您可以将其扩展到任意数量的参数,例如:

!:10-12

回答by Antonio Mano

In the command line you can press esc-.or alt+.

在命令行中,您可以按esc-.alt+.

It cycles through the last argumentof your previous commands.

它循环遍历您之前命令的最后一个参数

回答by Madisz

If you know the number given in the history for a particular command, you can pretty much take any argument in that command using following terms.

如果您知道特定命令的历史记录中给出的数字,您几乎可以使用以下术语在该命令中获取任何参数。

Use following to take the second argument from the third command in the history,

使用以下命令从历史记录中的第三个命令中获取第二个参数,

!3:2

!3:2

Use following to take the third argument from the fifth last command in the history,

使用以下命令从历史记录中的倒数第五个命令中获取第三个参数,

!-5:3

!-5:3

Using a minus sign, you ask it to traverse from the last command of the history.

使用减号,您要求它从历史记录的最后一个命令开始遍历。

回答by Johntron

!!:nwhere nis the 0-based position of the argument you want.

!!:nn您想要的参数的从 0 开始的位置在哪里。

For example:

例如:

echo 'one' 'two'
# "one two"

echo !!:2
# "two"

The !prefix is used to access previous commands.

!前缀用于先前访问命令。

Other useful commands:

其他有用的命令:

  • !$- last argument from previous command
  • !^- first argument (after the program/built-in/script) from previous command
  • !!- previous command (often pronounced "bang bang")
  • !n- command number nfrom history
  • !pattern- most recent command matching pattern
  • !!:s/find/replace- last command, substitute findwith replace
  • !$- 上一个命令的最后一个参数
  • !^- 上一个命令的第一个参数(在程序/内置/脚本之后)
  • !!- 上一个命令(通常发音为“bang bang”)
  • !n- 命令编号n来自history
  • !pattern- 最近的命令匹配 pattern
  • !!:s/find/replace- 最后一个命令,替换findreplace

More info on command history

有关命令历史记录的更多信息

回答by Johntron

!* runs a new command with all previous arguments.

!* 运行带有所有先前参数的新命令。

ls /tmp
cd !*
#you are now in /tmp