I’m trying to make a simple version of bash, but I’m facing a random behavior when unsetting the variable PWD:
before unsetting the PWD variable:
user:~$ env | grep PWD
PWD=/nfs/homes/user
OLDPWD=/nfs/homes/user
user:~$ echo $PWD
/nfs/homes/user
user:~$ echo $OLDPWD
/nfs/homes/user
after unsetting PWD:
user:~$ unset PWD
user:~$ env | grep PWD
OLDPWD=/nfs/homes/user
user:~$ echo $PWD
user:~$ echo $OLDPWD
/nfs/homes/user
after changing the current working directory:
user:~$ cd Desktop/
user:~/Desktop$ env | grep PWD
user:~/Desktop$ echo $PWD
/nfs/homes/user/Desktop
user:~/Desktop$ echo $OLDPWD
after changing the cwd the second time:
user:~/Desktop$ cd dir/
user:~/Desktop/dir$ env | grep PWD
OLDPWD=/nfs/homes/user/Desktop
user:~/Desktop/dir$ echo $PWD
/nfs/homes/user/Desktop/dir
user:~/Desktop/dir$ echo $OLDPWD
/nfs/homes/user/Desktop
this behavior seems weird to me, and I would need to hard code it to have the same behavior in my code but I don’t like that, is there any logical explanation of what is happening under the hood and what should I do to reproduce the same behavior?