On Tue, Oct 03, 2017 at 10:29:08PM +0200, Mikulas Patocka wrote: > If $OLDPWD points to a non-existing directory, 'cd -' will fail. > But if we clear $OLDPWD, 'cd -' will fail too (with just different message). [...]
I performed the following tests: dualbus@ubuntu:~$ for sh in mksh ksh93 dash zsh posh bash; do echo $sh \| $(OLDPWD=/invalid $sh -c 'echo "$OLDPWD | "; cd -; echo " | $?"' 2>&1); done mksh | /invalid | mksh: cd: /invalid: No such file or directory | 2 ksh93 | /invalid | ksh93: cd: /invalid: [No such file or directory] | 1 dash | /invalid | dash: 1: cd: can't cd to /invalid | 2 zsh | /home/dualbus | | 0 posh | /invalid | posh: cd: /invalid - No such file or directory | 1 bash | | bash: line 0: cd: OLDPWD not set | 1 dualbus@ubuntu:~$ for sh in mksh ksh93 dash zsh posh bash; do echo $sh \| $(OLDPWD=/tmp $sh -c 'echo "$OLDPWD | "; cd -; echo " | $?"' 2>&1); done mksh | /tmp | /tmp | 0 ksh93 | /tmp | /tmp | 0 dash | /tmp | /tmp | 0 zsh | /home/dualbus | | 0 posh | /tmp | /tmp | 0 bash | /tmp | /tmp | 0 So: - Bash is the only major shell that performs stat() on OLDPWD to see if it's going to import it from the environment or initialize it to an empty value. - Zsh doesn't import OLDPWD from the environment (bash's old behavior) - The rest of the shells (mksh, ATT ksh, dash, posh) import OLDPWD from the environment unconditionally. - Mikulas already made the argument above of why this change wouldn't break existing scripts that rely on bash performing the stat(). - I don't see a way around this behavior using existing bash features. The value of OLDPWD can't be reset via BASH_ENV or other initialization mechanisms. Sure, `unset OLDPWD; program' might work, but I wouldn't call that a solution. Given the above, I've changed my mind, and I think this patch should be fine :-)