コマンドラインで前のコマンドの文字列を置き換える方法は?

コマンドラインで前のコマンドの文字列を置き換える方法は?

コマンドを実行してから、同じコマンドを再実行して文字列を1つだけ変更する必要があります。

たとえば、次のコマンドを実行します。

$ ./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code1 -t query

そこからコマンド履歴に戻ることなく(上矢印を介して)、または別の文字列code1に置き換える必要があります。mycode

Bashでできますか?

ベストアンサー1

スクリプトの名前を変更しましたが、次のオプションがあります。

$ ./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code1 -t query

スクリプトを実行した後、次を使用します。

$ ^code1^code2

...結果は次のとおりです。

./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code2 -t query

man bash「イベントインジケータ」を検索します。

^文字列1^文字列2^

迅速な交換。最後のコマンドを繰り返して、string1をstring2に置き換えます。 !!:s/string1/string2/ と同じ

@slmの回答で、今学んだグローバル置換を追加するように編集されました。https://unix.stackexchange.com/a/116626/117549:

$ !!:gs/string1/string2

これは次のように言います。

!! - recall the last command
g - perform the substitution over the whole line
s/string1/string2 - replace string1 with string2

おすすめ記事