about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--README4
-rw-r--r--Src/exec.c10
-rw-r--r--Test/B07emulate.ztst22
4 files changed, 36 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 0e8a33f7c..c98923055 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,10 @@
 
 2021-04-10  dana  <dana@dana.is>
 
+	* brian m. carlson: 47794: README, Src/exec.c,
+	Test/B07emulate.ztst: exec: run final pipeline command in a
+	subshell in sh mode
+
 	* unposted (see 48415): README: Document incompatibility
 	caused by workers/43928
 
diff --git a/README b/README
index f47adfe7c..5e3b8cc59 100644
--- a/README
+++ b/README
@@ -95,6 +95,10 @@ right thing, but this should be viewed as an unsupported hack.)
 The XTRACE option is now disabled while running user-defined completion
 widgets.  See NEWS.
 
+emulate sh: When zsh emulates sh, the final command in a pipeline is now run in
+a subshell.  This differs from the behavior in the native (zsh) mode, but is
+consistent with most other sh implementations.
+
 Incompatibilities between 5.7.1 and 5.8
 ---------------------------------------
 
diff --git a/Src/exec.c b/Src/exec.c
index 2301f85ad..6f09e0d9f 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2901,11 +2901,13 @@ execcmd_exec(Estate state, Execcmd_params eparams,
 	    pushnode(args, dupstring("fg"));
     }
 
-    if ((how & Z_ASYNC) || output) {
+    if ((how & Z_ASYNC) || output ||
+	(last1 == 2 && input && EMULATION(EMULATE_SH))) {
 	/*
-	 * If running in the background, or not the last command in a
-	 * pipeline, we don't need any of the rest of this function to
-	 * affect the state in the main shell, so fork immediately.
+	 * If running in the background, not the last command in a
+	 * pipeline, or the last command in a multi-stage pipeline
+	 * in sh mode, we don't need any of the rest of this function
+	 * to affect the state in the main shell, so fork immediately.
 	 *
 	 * In other cases we may need to process the command line
 	 * a bit further before we make the decision.
diff --git a/Test/B07emulate.ztst b/Test/B07emulate.ztst
index 7b1592fa9..45c39b51d 100644
--- a/Test/B07emulate.ztst
+++ b/Test/B07emulate.ztst
@@ -276,3 +276,25 @@ F:Some reserved tokens are handled in alias expansion
 0:--emulate followed by other options
 >yes
 >no
+
+  emulate sh -c '
+  foo () {
+    VAR=foo &&
+    echo $VAR | bar &&
+    echo "$VAR"
+  }
+  bar () {
+    tr f b &&
+    VAR="$(echo bar | tr r z)" &&
+    echo "$VAR"
+  }
+  foo
+  '
+  emulate sh -c 'func() { echo | local def="abc"; echo $def;}; func'
+  emulate sh -c 'abc="def"; echo | abc="ghi"; echo $abc'
+0:emulate sh uses subshell for last pipe entry
+>boo
+>baz
+>foo
+>
+>def