我知道Esc +。给出最后一个命令的最后一个参数。
但我对最后一个命令的第一个参数感兴趣。 是否有一个键绑定来这样做?
在同一行中,是否有从最后一个命令中获取第n个参数的通用方法? 我知道在bash脚本中,您可以使用$0,$1等,但这些在命令行上不起作用。
同样,如果遍历前面命令的第0个参数,就像我们连续按Esc +对最后一个参数所做的那样,会怎么样呢?
我知道Esc +。给出最后一个命令的最后一个参数。
但我对最后一个命令的第一个参数感兴趣。 是否有一个键绑定来这样做?
在同一行中,是否有从最后一个命令中获取第n个参数的通用方法? 我知道在bash脚本中,您可以使用$0,$1等,但这些在命令行上不起作用。
同样,如果遍历前面命令的第0个参数,就像我们连续按Esc +对最后一个参数所做的那样,会怎么样呢?
当前回答
$获取上一个命令行参数的最后一个元素。
其他回答
您还可以从历史记录中的任何命令获取参数! $ echo a b c d f g A b c d e f g $ echo build/libs/jenkins-utils-all-0.1.jar 建立/ libs / jenkins-utils-all-0.1.jar $ history | tail -5 601 echo build/libs/jenkins-utils-all-0.1.jar 602历史|尾巴-10 603回声a b c d e f g 604 echo build/libs/jenkins-utils-all-0.1.jar 605历史|尾巴-5 $ echo !-3:4 回声d d $ echo !604:1 构建/ libs / jenkins-utils-all-0.1.jar回响 建立/ libs / jenkins-utils-all-0.1.jar
如果你使用的是mac,你会倾向于使用ctrl+字母来扩展字符。我在终端(iTerm2)中设置了空格右键选项键,并将其定义为meta。这意味着我使用键按字导航,并从以前的命令中提取参数。
$获取上一个命令行参数的最后一个元素。
^将得到第一个参数,!$将得到最后一个参数,!:n将得到第n个元素。
我非常喜欢@larsmans的回答,所以我必须多学一些。添加这个 回答帮助其他人找到手册页部分,并知道要做什么 谷歌:
$ man -P 'less -p ^HISTORY\ EXPANSION' bash
<...>
Word Designators
Word designators are used to select desired words from the event.
A : separates the event specification from the word designator.
It may be omitted if the word designator begins with a ^, $, *, -,
or %. Words are numbered from the beginning of the line, with the
first word being denoted by 0 (zero). Words are inserted into the
current line separated by single spaces.
0 (zero)
The zeroth word. For the shell, this is the command word.
n The nth word.
^ The first argument. That is, word 1.
$ The last argument.
% The word matched by the most recent ‘?string?’ search.
x-y A range of words; ‘-y’ abbreviates ‘0-y’.
* All of the words but the zeroth.
This is a synonym for ‘1-$’.
It is not an error to use * if there is just one word in
the event; the empty string is returned in that case.
x* Abbreviates x-$.
x- Abbreviates x-$ like x*, but omits the last word.
If a word designator is supplied without an event
specification, the previous command is used as the event.