zsh - 如何在命令行中获得前一个命令的第 n 个参数?

标签 zsh

如果您在交互式 shell 中并键入以下内容:

echo this is it

然后你可以扩展第一个参数:
echo !^    #=> echo this

或者您可以扩展最后一个参数:
echo !$    #=> echo it

但现在我想知道:

我将如何访问第 n 个参数?我看了a related bash question ,但似乎只有在脚本中才有效,因为 !n只是浏览我的命令历史(而不是我的参数历史) - 例如
    echo !1 #=> echo ls

这是有道理的,因为
    history | grep -E '^\s+1 ' #=> 1  ls

但我想要的是echo !(some correct index) #=> echo is

最佳答案

这边走:

~ $ echo this is it
~ $ echo !!:2
echo is
is
!!:n是第 n 个参数!!:n-$是从第 n 个到最后一个的 args

注:!!扩展到最后一个命令

根据 OP 的编辑( moved ):

倒数第二个命令的第二个参数:
~ $ echo foo bar baz # This one is the target
foo bar baz
~ $ echo catz ratz batz
catz ratz batz
~ $ echo !-2:2
echo bar
bar
!-n扩展到在当前命令之前有 'n' 个命令的命令。

注:!-1!!是相同的。

关于zsh - 如何在命令行中获得前一个命令的第 n 个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277931/

相关文章:

shell - Zsh 选项卡补全 : Is there any plugin or feature that can allow for a MRU tab-completion scheme?

terminal - 如果前台进程,Zsh 新终端选项卡在主文件夹而不是 CWD 中打开

php - zsh: 错误的数学表达式:操作数预期在 `' HTTP_HOST...'

python - Catalina zsh shell 更改破坏了 jupyter 笔记本路径

zsh - 如何为 zsh 设置 Vi 编辑模式

bash - zsh 中的 checkwinsize 等效项?

git - 仅 Git 命令的制表符完成卡住

zsh - 为什么此代码在传递给 `zsh -n` 而不是传递给 `zsh` 时抛出错误?

zsh - 如何解决zsh compinit : insecure directories issue on MacOS (other solutions failed)

ubuntu - E185 : Cannot find color scheme solarized