Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OLDPWD unset when bash starts

23 views
Skip to first unread message

John Wiersba

unread,
Nov 18, 2015, 3:08:08 PM11/18/15
to bug-...@gnu.org
Why does bash clear OLDPWD when a child script is started?

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>"'

<>
$ uname -a
Linux myserver 2.6.32-504.8.1.el6.x86_64 #1 SMP Fri Dec 19 12:09:25 EST 2014
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
$ bash --version | head -1

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Can bash be fixed to preserve the value of any OLDPWD in its initial environment, like it does with PWD?

Thanks!
-- John Wiersba

John Wiersba

unread,
Nov 19, 2015, 9:50:11 AM11/19/15
to bug-...@gnu.org, ba...@packages.debian.org
From: jrw3...@yahoo.com
To: bug-...@gnu.org,ba...@packages.debian.org
Subject: OLDPWD unset when bash starts

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='l$
uname output: Linux john-mint-mate-17 3.13.0-37-generic #64-Ubuntu SMP Mon Sep $
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.3
Patch Level: 11
Release Status: release

Description:

Why does bash clear OLDPWD when a child script is started?

OLDPWD is exported and passed to any children, but bash apparently clears OLDPWD whenever a child script is started: Can bash be fixed to preserve the value of any OLDPWD in its initial environment, like it does with PWD?


This appears to happen on all 3.x and 4.x versions of bash.
Repeat-By:

Chet Ramey

unread,
Nov 26, 2015, 12:41:23 PM11/26/15
to John Wiersba, bug-...@gnu.org, chet....@case.edu
On 11/18/15 2:44 PM, John Wiersba wrote:
> 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.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU ch...@case.edu http://cnswww.cns.cwru.edu/~chet/

John Wiersba

unread,
Nov 26, 2015, 5:14:50 PM11/26/15
to chet....@case.edu, bug-...@gnu.org
Thanks, Chet!


From: Chet Ramey <chet....@case.edu>
To: John Wiersba <jrw3...@yahoo.com>; "bug-...@gnu.org" <bug-...@gnu.org>
Cc: chet....@case.edu
Sent: Thursday, November 26, 2015 12:41 PM
Subject: Re: OLDPWD unset when bash starts
0 new messages