3 ms·
pushd / popd are also useful in scripts. For some reason they don't seem to be very well known and I rarely see them in practice. $ pwd /foo $ pushd /ba
by 5e92cb50239222b 4y ago
pushd / popd are also useful in scripts. For some reason they don't seem to be very well known and I rarely see them in practice.
$ pwd
/foo
$ pushd /bar && pwd
/bar
$ pushd /quux && pwd
/quux
$ popd && pwd
/bar
$ popd && pwd
/foo
So it's basically a stack.
- GuB-42 4y agoPushd / popd are not POSIX and some shells like ash/dash don't support them. I would avoid them unless you are targeting a specific shell you know supports them (ex: bash).
- harry8 4y ago"targeting" seems like an odd thing to say there. This is an interactive shell command. Does the shell you are using interactively support it seems like a more interesting concern? We don't have to be portable typing commands into a shell. :-) Never seen {push,pop}d in a shell script.
- GuB-42 4y agoGP was specifically talking about scripts, and I see the utility. As an interactive feature, I use the "autopushd" option in zsh, which make all "cd" behave like pushd, and you can also use "cd -n" where "n" is the number of levels you want to go up the stack (so "cd -" is equivalent to "cd -1")
- harry8 4y agooh wow. How did I not see that? Sorry!