bash:引用当前命令的先前参数 [英] bash: refer to previous arguments of current command

查看:33
本文介绍了bash:引用当前命令的先前参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想执行以下操作:

For example, I would like to do the following:

mv xxxx xxxx.bak

我知道我可以改用以下内容:

I know I could use the following instead:

mv xxxx{,.bak}

我认为这不是直接的

.如果我可以这样做,那就太好了

which I think is not direct somehow. It would be wonderful if I can do this like:

mv xxxx $1.bak

有时我们会需要这样:

echo xxxx yyyy $1.suffix

我知道我们可以使用!:n"来引用 previous 命令的参数,我可以吗?而是同样引用 current 命令的参数吗?

I know we can refer to arguments of the previous command using "!:n", can I instead refer to arguments of the current command likewise?

顺便说一句,我想直接在shell中(交互地)进行操作.谢谢.

BTW, I want to do it directly in shell(interactively). Thanks.

推荐答案

当前命令行由!#引用.

mv xxxx !#:1.bak

如果您尚未使用 histverify 选项,我建议启用它,这样您就有机会在实际执行之前对历史扩展的结果进行校对或编辑.为此:

I recommend enabling the histverify option if you aren't already using it, so you have a chance to proofread or edit the results of the history expansion before actually executing it. To do so:

shopt -s histverify

或者,如果您不想启用该选项,而只想验证一个命令,请使用:p 修饰符打印扩展而不是执行:

Or, if you don't want to enable that option and just want to verify a single command, use the :p modifier to print the expansion instead of executing it:

$ mv xxx !#:1:p.bak
mv xxx xxx.bak
$

这篇关于bash:引用当前命令的先前参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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