Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: inherit OLDPWD from environment variable



> > On 17/07/2024 08:59 BST Franklin Yu <franklinyu@xxxxxxxxxxx> wrote:
> > Hi folks, this could be either a bug report or feature request.
> >
> > When I start Zsh with $OLDPWD set to a path different from $PWD, Zsh overwrites
> > $OLDPWD with $PWD. Is this expected, or is it a side effect of making sure
> > $OLDPWD is non-empty?
> 
> I think it's just making sure OLDPWD is set to something on
> initialisation.  The only comment says "initialize `OLDPWD' = `PWD'".
> 
> I can't think of any obvious objection to importing a value from the
> environment and only setting the variable to $PWD if that was empty.
> Until the directory within the shell changes its value is pretty
> arbitrary.
> 
> pws

Good to hear that this idea works for you. Shall we adopt this idea, then? I
think it is as simple as:

diff --git a/Src/init.c b/Src/init.c
index ec21521b1..092273228 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1245,7 +1245,9 @@ setupvals(char *cmd, char *runscript, char *zsh_name)
        pwd = metafy(zgetcwd(), -1, META_DUP);
     }

-    oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */
+    oldpwd = zgetenv("OLDPWD");
+    if (oldpwd == NULL || oldpwd[0] == '\0')
+        oldpwd = ztrdup(pwd);  /* initialize `OLDPWD' = `PWD' */

     inittyptab();     /* initialize the ztypes table */
     initlextabs();    /* initialize lexing tables    */



Messages sorted by: Reverse Date, Date, Thread, Author