OLDPWD is exported and passed to any children, but bash apparently clears OLDPWD whenever a child script is started:
$ cd /etc
$ cd
$ perl -e 'print "<$ENV{OLDPWD}>n"'
</etc>
$ ksh -c 'echo "<$OLDPWD>"'
</etc>
$ bash -c 'echo "<$OLDPWD>"'
<>
Any way to work around that other than creating an alias or dotting the child script or exporting some other variable with the same value, whenever I want to use $OLDPWD in a child script?
** UPDATE 2015/11/26 **
I filed a bash bug report and got this reply from Chet Ramey, the bash maintainer:
Why does bash clear OLDPWD when a child script is started?
Because a new shell does not have a `previous working directory’.
It’s supposed to be set by cd, and if you haven’t executed cd, you
don’t have one.It seems reasonable to inherit OLDPWD if it names a directory, in
the same way that the shell inherits PWD if it names the current
directory, so we’ll try that for the next bash version.