![以前に入力した引数を参照するbash魔法はありますか? [コピー]](https://linux33.com/image/101177/%E4%BB%A5%E5%89%8D%E3%81%AB%E5%85%A5%E5%8A%9B%E3%81%97%E3%81%9F%E5%BC%95%E6%95%B0%E3%82%92%E5%8F%82%E7%85%A7%E3%81%99%E3%82%8Bbash%E9%AD%94%E6%B3%95%E3%81%AF%E3%81%82%E3%82%8A%E3%81%BE%E3%81%99%E3%81%8B%EF%BC%9F%20%5B%E3%82%B3%E3%83%94%E3%83%BC%5D.png)
この質問は以前に質問しました 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 histverify
Enterキーを押した後、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.