Skip to content

Commit dce8a09

Browse files
author
Andy Chu
committed
Export PWD and OLDPWD.
That is, the variables are not only set within the shell, but exported to child processes. This seems to be an undocumented feature of bash and dash? Found with env -i -- $sh -c env Tricky issue: it's hidden in spec tests with bin/osh, because bin/osh is a shell wrapper run with dash (/bin/sh)! It only showed up in the tests against the release binary, where we use a symlink.
1 parent 952e96e commit dce8a09

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

devtools/release.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/bin/bash
22
#
3+
# The big Oil release process.
4+
#
35
# Usage:
46
# devtools/release.sh <function name>
57
#
68
# Pre-release:
9+
# demo/osh-debug.sh osh-for-release: Start a shell to dogfood
710
# opy/regtest.sh verify-golden, because that one tends to be flaky
811
# build/cpython-defs.sh {oil-py-names,filter-methods} # regenerate C source
912
# (BUG: this depends on build/test.sh oil-tar and building the tarball)
@@ -36,6 +39,8 @@
3639
# MAYBE: ./local.sh test-release-tree if you want to preview it
3740
# $0 sync-old-tar (for releases.html)
3841
# $0 deploy-doc
42+
#
43+
# demo/osh-debug.sh analyze # see what you ran
3944
#
4045
# - Go to oilshell.org__deploy and "git add release/$VERSION".
4146
# - Go to oilshell.org repo and do ./deploy.sh site.

osh/builtin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ def Cd(argv, mem, dir_stack):
644644
util.error("cd %r: %s", real_dest_dir, posix.strerror(e.errno))
645645
return 1
646646

647-
state.SetGlobalString(mem, 'OLDPWD', pwd.s)
648-
state.SetGlobalString(mem, 'PWD', real_dest_dir)
647+
state.ExportGlobalString(mem, 'OLDPWD', pwd.s)
648+
state.ExportGlobalString(mem, 'PWD', real_dest_dir)
649649
dir_stack.Reset() # for pushd/popd/dirs
650650
return 0
651651

osh/state.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,14 @@ def Dump(self):
512512
return var_stack, argv_stack, debug_stack
513513

514514
def _InitDefaults(self):
515+
# For some reason this is one of few variables EXPORTED. bash and dash
516+
# both do it. (e.g. env -i -- dash -c env)
517+
ExportGlobalString(self, 'PWD', posix.getcwd())
518+
515519
# Default value; user may unset it.
516520
# $ echo -n "$IFS" | python -c 'import sys;print repr(sys.stdin.read())'
517521
# ' \t\n'
518522
SetGlobalString(self, 'IFS', split.DEFAULT_IFS)
519-
SetGlobalString(self, 'PWD', posix.getcwd())
520523

521524
# NOTE: Should we put these in a namespace for Oil?
522525
SetGlobalString(self, 'UID', str(posix.getuid()))
@@ -1099,7 +1102,7 @@ def SetArrayDynamic(mem, name, a):
10991102

11001103

11011104
def SetGlobalString(mem, name, s):
1102-
"""Helper for completion, $PWD, etc."""
1105+
"""Helper for completion, etc."""
11031106
assert isinstance(s, str)
11041107
val = value.Str(s)
11051108
mem.SetVar(lhs_expr.LhsName(name), val, (), scope_e.GlobalOnly)
@@ -1111,6 +1114,14 @@ def SetGlobalArray(mem, name, a):
11111114
mem.SetVar(lhs_expr.LhsName(name), value.StrArray(a), (), scope_e.GlobalOnly)
11121115

11131116

1117+
def ExportGlobalString(mem, name, s):
1118+
"""Helper for completion, $PWD, $OLDPWD, etc."""
1119+
assert isinstance(s, str)
1120+
val = value.Str(s)
1121+
mem.SetVar(lhs_expr.LhsName(name), val, (var_flags_e.Exported,),
1122+
scope_e.GlobalOnly)
1123+
1124+
11141125
def GetGlobal(mem, name):
11151126
assert isinstance(name, str), name
11161127
return mem.GetVar(name, scope_e.GlobalOnly)

spec/builtins.test.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ echo $PWD
2525
cd /
2626
cd $TMP
2727
echo "old: $OLDPWD"
28+
env | grep OLDPWD # It's EXPORTED too!
2829
cd -
29-
## stdout-json: "old: /\n/\n"
30+
## STDOUT:
31+
old: /
32+
OLDPWD=/
33+
/
34+
## END
35+
## BUG mksh STDOUT:
36+
old: /
37+
/
38+
## END
3039

3140
#### pwd
3241
cd /

0 commit comments

Comments
 (0)