100%個人的などうでもよい背景
業務でシェルスクリプトで書かれたツールを使う必要があり、ツールを作った人が書いたマニュアルを読んでいた。
マニュアルに書いてある手順のなかで
!mkdir
などと書かれた箇所があり、
このビックリは何だ?と思って調べた。
!の意味
man bashから抜粋
Event Designators
An event designator is a reference to a command line entry in the history list.
イベント指定子は、履歴リストのコマンドラインエントリへの参照です。
履歴リストって?→$history | less
で見られるやつ
! Start a history substitution, except when followed by a blank, newline, carriage return, = or
( (when the extglob shell option is enabled using the shopt builtin).
ヒストリ置換を開始します。ただし、空白、改行、キャリッジリターン、または(が後に続く場合は除く(shopt組み込み関数を使用してextglobシェルオプションが有効になっている場合)。!n Refer to command line n.
履歴リストのn番目のコマンドラインへの参照。
→n番目のコマンドを再実行する。!-n Refer to the current command line minus n.
履歴リストを現在のコメンドラインから遡ってn番目への参照。
→n個前のコマンドを再実行する。こっちの方が普通に使いそうか。!! Refer to the previous command. This is a synonym for ‘!-1’.
直前のコマンドへの参照。これは '!-1'と同義。!string
Refer to the most recent command starting with string.
一番最近のstringで始まるコマンド。
つまり、!mkdir
だと一番最近のmkdirコマンドを再実行する。!?string[?]
Refer to the most recent command containing string. The trailing ? may be omitted if string
is followed immediately by a newline.
一番最近のstringを含むコマンド。文字列の直後に改行が続く場合は、後ろの?は省略することができる。
([]は省略可能という意味で実際には入力しない)^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to
‘‘!!:s/string1/string2/’’ (see Modifiers below).
クイック置換。最後のコマンドを繰り返し、string1をstring2に置き換える。 '' !!:s / string1 / string2 / ''と等価。!# The entire command line typed so far.
現時点のコマンドライン全体。
個人的な結末
マニュアル上でコマンドの先頭に付いてた"!"は上記とは無関係で、
$とか#とかと同じで単にコマンドラインですよという意味を表す記号でしかなかった。
要するに、勘違いで、そのツールを使う上で上記の内容はとくに関係がなかった。
(マニュアルをきちんと読むと履歴を利用するようなシチュエーションでもないし、全部のコマンドの前に!がついていた)
ところで、#はrootで$はそれ以外のユーザーの場合の表示だと聞いたことがあるけど、じゃあ!は何か特定の状況を表す意味があるんだろうか?