以前に入力した引数を参照するbash魔法はありますか? [コピー]

以前に入力した引数を参照するbash魔法はありますか? [コピー]

この質問は以前に質問しました bashコンソールで現在入力されているパラメータをどのように繰り返すのですか?


シェルでファイル名を少し変更したい場合が多いです。たとえば、次のようになります。

$ 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

魔法は2つの部分に分けられます。

まず、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 histverifyEnterキーを押した後、Enterキーを押して再実行する前に、歴史的な拡張の効果を見る機会があります。

他の例:

$ 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.

おすすめ記事