Tuesday, September 1, 2015

A note on pushd and popd commands

Users of Linux shell are familiar with pushd & popd commands. But wait a minute, how does it work? How does it remember the directories which it traversed. Just a stack functionality right?
As you are aware, bash does fork+exec on the commands it runs and hence it is impossible for pushd & popd to store the directory list if spawned afresh. The solution is none other than the method used by bash for 'cd' command. Yes, the commands 'popd' & 'popd' too nestle within the bash executable ;). Of-course the concept of shared memory can pitch-in but why complicate when there is simpler solution ;)

The experiment as mentioned in my 'cd' post

heramba@heramba-MS-7640 ~ $ which pushd
heramba@heramba-MS-7640 ~ $ which ls
/bin/ls
heramba@heramba-MS-7640 ~ $ which popd