有任何 bash 魔法可以引用先前輸入的參數嗎?

有任何 bash 魔法可以引用先前輸入的參數嗎?

這個問題是一個變體之前問過 如何在 bash 控制台上重複目前輸入的參數?


很多時候,我發現自己想在 shell 中稍微重命名一個檔名,例如:

$ mv test_1.py _test_1.py

或者

$ mv test_1.py test_1.py.org

我可以使用中的建議如何在 bash 控制台上重複目前輸入的參數?, 但

有沒有重擊魔法這將允許我只引用之前輸入的參數?

例如,如果魔法是$M,那麼 - 對於上面的內容 - 我會使用:

$ mv test_1.py _$M.py
$ mv test_1.py $M.org

答案1

魔法分為兩部分。

首先,echo 被別名 e(如果您不需要別名,echo 也可以工作);其次,我們使用「大括號擴充」:

$ e mv {,_}test_1.py              # Try to see if it works as expected.
mv test_1.py _test_1.py

          # If the arguments are what you want:

$ mv {,_}test_1.py                # Press ↑ (up arrow), remove `e`

$ !*                              # OR: Type `!*` --> last line after arg 0.
$ mv {,_}test_1.py                # If `histverify` is set.

!* 可以用空格擴展,如果您啟用魔法空間或者如果你shopt -s histverify按下回車鍵後,您將有機會在按下回車鍵(再次)執行之前查看歷史擴展的效果。

另一個例子:

$ e mv test_1.py{,.org}
mv test_1.py test_1.py.org        # The result of the brace expansion.
                                  # Review it, and if it is ok:
$ !*                              # type !* (use either space or enter)
                                  # That will depend on how you setup it.

$ mv test_1.py{,.org}        # The command appear again (enter to execute).
$

還有歷史擴展,!#這意味著到目前為止輸入的命令行,並選擇第一個命令:1。如果啟用了 magic-space,則鍵入mv test1.py !#:1並按下空白鍵,命令將變更:

$ mv test_1.py !#:1                # Press space.
$ mv test_1.py test_1.py           # The command line change to this.
$ mv test_1.py test_1.org          # Edit and press enter to execute.

相關內容