一つの共有アカウントを使用した場合、history がどのように残るかずっとモヤモヤしていたので、調査してみました。
共有アカウントでだけでなく、同一ユーザから複数のシェルを起動した時も同じ疑問が残るかと。
環境
$ cat /etc/redhat-release
CentOS release 6.2 (Final)
$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
以下略
いろいろやってみた
パターンA - ログイン時間が重ならないパターン
shell(1)
$ history -c
$ history -w
$ echo nya
nya
$ exit
shell(2)
$ history
1 history -w
2 history -w
3 echo nya
4 exit
$ echo wan
wan
$ exit
shell(3)
$ history
1 history -w
2 history -w
3 echo nya
4 exit
5 echo wan
6 exit
パターンB - シェルが後半だけ重なってるパターン
shell(1)
$ history -c
$ history -w
$ echo nya
nya
$ exit
shell(2)
$ history
1 history -w
$ echo wan
wan
$ exit
shell(3)
$ history
1 history -w
2 history -w
3 echo nya
4 exit
5 echo wan
6 exit
→ nya も wan も見えてるなぁ
パターンC - シェル起動中に別シェルが起動・終了するパターン
shell(1)
$ history -c
$ history -w
$ echo nya
nya
$ history
1 history -w
2 echo nya
$ exit
→別シェルのコマンドはリアルタイムには反映されない
shell(2)
$ history
1 history -w
$ echo wan
wan
$ exit
shell(3)
$ history
1 history -w
2 echo wan
3 exit
4 history -w
5 echo nya
6 exit
→一旦 exit したら2つとも見えてる
パターンD - 同一シェル内でシェルを起動した
shell(1)
$ history -c
$ history -w
$ echo nya
nya
$ bash
$ history
1 history -w
$ echo wan
wan
$ exit
exit
$ history
1 history -w
2 echo nya
3 bash
・子シェルを起動して終了しても子シェルの history は見えない
shell(2)
$ history
1 history -w
2 echo wan
3 exit
4 history -w
5 echo nya
6 bash
7 exit
・一旦 exit すると追加されてる。ただ順番が入れ替わってる。時系列機には先に nya を売っているが、子シェルの wan が古い履歴に入ってる結果となった。うーん、これはどういう挙動なんだろう。
最後に
・以前作業中に履歴が作業履歴が history に残らなかった記憶があったので調べてみたが、結局なんらかの形で履歴は残ってしまった。あれはなんだったんだろう。
・ぐぐってみたけど、やはり session 終了時に、 .bash_history に save (追記)される模様。
gnome-terminal(Linux 上の GUI terminal) を mouse でバツ切りしたり、reboot すると save されない情報もあった。へー。
ref. http://linux-training.be/files/books/html/fun/ch13s07.html