@@ -2866,11 +2866,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.
@@ -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