如何从前面的bash命令获取倒数第二个参数? (在交互式bash shell中) [英] How to get second-to-last argument from previous bash command? (in an interactive bash shell)

查看:1915
本文介绍了如何从前面的bash命令获取倒数第二个参数? (在交互式bash shell中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在bash交互式shell中轻松获取上一个命令的倒数第二个(倒数第二个)单词/参数?我经常在后台运行命令,我想获得在& 之前指定的文件,例如

How can I easily get the second-to-last (penultimate) word/argument from the previous command in a bash interactive shell? I often run commands in the background, and I would like to get the file that was specified before the &, e.g.,

  % echo foo > /tmp/foo &
  % cat !$
  % &

在上面的示例中,!$ 最后一个字,& 。但我想要第二个到最后一个参数, / tmp / foo

In the example above, !$ gives the last word, &. But I want the second-to-the-last argument, /tmp/foo




请注意,可以使用范围如! - 1:3 的字标志符,但这对于具有大量字的命令是不切实际的在那里不太明显有多少字,例如



Note that it is possible to use word designators with a range like !-1:3, but this is impractical for a command with a large number of words where it's not quickly obvious how many words there are, e.g.,

% (set -x; date; pwd; git status; git diff;  git log | tail -30; date; args=--verbose time make test; date) >& /tmp/log/make.test.20150122-Thu-0834 &
% echo !-1:30
/tmp/log/make.test.20150122-Thu-0834

上面的例子工作,但你必须计数并知道你想要的单词是第30个单词,这是耗时和容易出错的。

The example above works, but you have to count and know that the word you want is the 30th word, which is time-consuming and error-prone.

有一个简单的方法来获得倒数第二个字吗?

Is there an easy way to get the second-to-last (penultimate) word?

(注意,这个问题是指前一个命令,而不是从命令行传递给shell脚本的参数,因为这里的一些答案和注释是指。)

(Note that this question refers to the arguments of a previous command in an interactive shell, and not to arguments passed to a shell script from the command-line, as some answers and comments here are referring to.)

推荐答案

交互地,您可以使用 esc - 1 获取上一个命令行的倒数第二个参数。

Interactively, you can get the second to last argument of the previous command line with esc - 1 esc . with the default bindings.

更普遍的是,序列 esc 从上一个命令行获取最终令牌,您可以向其传递一个数字参数以指定不同的参数(例如, esc 1 esc 获取第一个参数,并且 esc 0 esc >获取命令本身)。

More generally, the sequence esc . gets the final token from the previous command line, and you can pass it a numeric argument to specify a different one (for example, esc 1 esc . gets the first argument, and esc 0 esc . gets the command itself).

esc 是Meta的关键绑定之一;在许多现代键盘上,您也可以使用 Alt 作为Meta(同时按下它,而不是作为前缀修饰符)。我喜欢 esc 作为元,因为当我的肌肉记忆学到这些东西,我们没有没有(可靠,一致的)Alt键,它仍然可移植到VT100和Sun键盘;和至少在我现在的键盘(Mac OSX Yosemite),例如。 alt - - 除了指定一个负数字参数外。

esc is one of the keybindings for Meta; on many modern keyboards, you can use Alt as Meta as well (you press it at the same time, not as a prefix modifier). I prefer esc as meta because when my muscle memory learned these things, we didn't have no (reliable, consistent) Alt key, and it's still portable all the way to VT100 and Sun keyboards; and at least on my current keyboard (Mac OSX Yosemite) e.g. alt-- does something else than specify a negative numeric argument.

/ p>

From a previous compound command like this

echo moo; echo bar

序列 esc 2 <

我确定有一个方法与 code>!历史扩展,但我非常喜欢看我在做什么。此机制将您要引用的文本带入当前命令行,因此,如果您也喜欢,您可以编辑它。

I'm sure there is a way with ! history expansion as well, but I vastly prefer to see what I'm doing. This mechanism brings you the text you want to refer to into your current command line, so you can edit it if you like as well.

这篇关于如何从前面的bash命令获取倒数第二个参数? (在交互式bash shell中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆