LoginSignup
19
20

More than 5 years have passed since last update.

pushdがなぜ混乱を招いているのか。

Last updated at Posted at 2014-07-03

概要

pushdとpopdが地味に便利なんだけど、ちょいちょい使い方を微妙に間違えて「あれ?」ってなるのでメモ。なんかいつも違和感を持ちながら使っていたが、実際これ、混乱を招きやすいと思う。

メモ

基本は、pushdをしたら指定した先にcdされる。このときのカレントディレクトリがpushされる。

$ pwd
/home/tarr
$ pushd /usr/local/
/usr/local ~
$ pwd
/usr/local

飛んだ先でcdとかをした後にpopdすると、pushdした時のdirectoryに飛ぶ。この時は/home/tarr

$ cd dev/leofs/
$ pwd
/usr/local/dev/leofs
$ popd
~
$ pwd
/home/tarr

いろいろとpushdしまくってみた。

$ cd 
$ pushd /usr/local/
/usr/local ~
$ cd erlang/
$ pushd /usr/local/dev/leofs/
/usr/local/dev/leofs /usr/local/erlang ~
$ pushd /usr/local/games/
/usr/local/games /usr/local/dev/leofs /usr/local/erlang ~
$ pushd /etc/
/etc /usr/local/games /usr/local/dev/leofs /usr/local/erlang ~

ここで気づいたんだけど、pushdした時に、pushされているdirectoryリストが表示されていて、その先頭に今回の移動先のdirectory名も表示されている。でも、実際はそのdirectoryはまだpushされていなくて、cdとかして他のdirectoryに移動すると、pushdが使われた時のcurrent directoryがpushされる。
結構連続してpushdをするようなシチュエーション(シェルスクリプトを書いているときの動作シミュレーションだとか)ではpushdで指定した先が、次のpushdでも指定されてそのままpushされるように見える。そして、pushdをしたタイミングの出力が現在stuckされているもののリストに見えてしまう。

結論

  • pushdでpushされるのはcurrent directory
  • pushdをした際の出力は現在pushされているものと、pushdしたものの引数となっているターゲットディレクトリ
  • しかし、そのターゲットディレクトリはpushされていない。
  • 上記が混乱を招いていた。

2014/07/03 16:15 追記

コメントで指摘されたのがきっかけで気づいたんだけど、出力結果は以下のようになっている。

$ pushd [target dir]
[current dir] [stack dir1] [stack dir2] [stack dir3]

こうなっているので、pushdをした結果、[target dir]が[current dir]と同じになっているので、[target dir]も出力されたようにみえる。これは、pushdでstackされている内容を表示するdirsコマンドで確かめられる。

$ cd /etc/
$ dirs
/etc ~ /usr/lib /usr/local/etc
$ cd /usr/local/erlang/
$ dirs
/usr/local/erlang ~ /usr/lib /usr/local/etc

僕の感覚だと、dirsが「pushdの中身を表示するコマンド」だったら、current directoryを同列で表示するのに違和感がある。たぶんそもそもdirectoryとは何かとか、current directoryとは何かとかを理解している人からしたら、見当外れなことを書いていると思う。きっと凄腕エンジニアが教えてくれると思う。

2014/07/03 16:30 追記

そうか、そもそもpushdは「current directoryをstackに保存」するものなのか。すべてがcurrent directoryということだと

$ pushd [next current dir]
[current dir] [current dir2] [current dir3] [current dir4]

こういうことなのか。当然stackの一番上で見えているcurrent dirは変わるということか

19
20
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
20