NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/55320: /bin/sh redirection in functions does not match other shells



>Number:         55320
>Category:       bin
>Synopsis:       /bin/sh redirection in functions does not match other shells
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat May 30 15:55:00 +0000 2020
>Originator:     Jan Schaumann
>Release:        NetBSD 8.0
>Organization:
	
>Environment:
$ ident /bin/sh
/bin/sh:
     $NetBSD: crt0.S,v 1.3 2011/07/01 02:59:05 joerg Exp $
     $NetBSD: crt0-common.c,v 1.14 2016/06/07 12:07:35 joerg Exp $
     $NetBSD: crti.S,v 1.1 2010/08/07 18:01:35 joerg Exp $
     $NetBSD: crtbegin.S,v 1.2 2010/11/30 18:37:59 joerg Exp $
     $NetBSD: alias.c,v 1.15 2014/06/18 18:17:30 christos Exp $
     $NetBSD: arith_token.c,v 1.4.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: arithmetic.c,v 1.2.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: cd.c,v 1.47.6.2 2017/07/23 14:58:14 snj Exp $
     $NetBSD: echo.c,v 1.14 2008/10/12 01:40:37 dholland Exp $
     $NetBSD: error.c,v 1.39.8.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: eval.c,v 1.140.2.3 2018/07/13 14:29:15 martin Exp $
     $NetBSD: exec.c,v 1.47.2.3 2018/07/13 14:29:15 martin Exp $
     $NetBSD: expand.c,v 1.110.2.4 2018/07/13 14:32:01 martin Exp $
     $NetBSD: histedit.c,v 1.48.8.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: input.c,v 1.56.2.2 2017/08/09 05:35:18 snj Exp $
     $NetBSD: jobs.c,v 1.85.2.2 2017/11/17 14:56:52 martin Exp $
     $NetBSD: mail.c,v 1.16.90.1 2017/06/05 08:10:24 snj Exp $
     $NetBSD: main.c,v 1.70.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: memalloc.c,v 1.29.62.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: miscbltin.c,v 1.44 2017/05/13 15:03:34 gson Exp $
     $NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $
     $NetBSD: options.c,v 1.49 2017/05/29 14:03:23 kre Exp $
     $NetBSD: parser.c,v 1.132.2.4 2018/05/06 09:32:57 martin Exp $
     $NetBSD: redir.c,v 1.57.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: show.c,v 1.42.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: trap.c,v 1.40.2.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: output.c,v 1.36.2.1 2017/11/23 13:26:01 martin Exp $
     $NetBSD: var.c,v 1.55.2.2 2017/08/31 11:43:44 martin Exp $
     $NetBSD: test.c,v 1.41 2016/09/05 01:00:07 sevan Exp $
     $NetBSD: kill.c,v 1.27.36.1 2017/07/23 14:58:13 snj Exp $
     $NetBSD: syntax.c,v 1.3.26.1 2017/07/23 14:58:14 snj Exp $
     $NetBSD: printf.c,v 1.37.8.1 2018/07/13 15:58:25 martin Exp $
     $NetBSD: crtend.S,v 1.1 2010/08/07 18:01:34 joerg Exp $
     $NetBSD: crtn.S,v 1.1 2010/08/07 18:01:35 joerg Exp $

Architecture: x86_64
Machine: amd64
>Description:

When using /bin/sh, it appears that a stderr redirection inside a
function affects subsequent calls when it ought to only apply to the
statements within that function.

>How-To-Repeat:

$ cat >/tmp/a.sh <<"EOF"
f() { :; }
g() { f >&2; }

v=$(g; echo foo; )

echo "v=$v"
EOF
$ 

Expected output:

$ sh /tmp/a.sh
v=foo

Actual output:
$ sh /tmp/a.sh
foo
v=
$ sh /tmp/a.sh 2>/dev/null
v=
$ 


That is, the ">&2" redirection applied to the invocation of f()
inside g() appears to continue to be applied _after_
g() has been invoked.

This is different in bash, zsh, and ksh:

$ ksh /tmp/a.sh 2>/dev/null
v=foo


Note: if we replace the call to f() with e.g., a call to a command
or even a shell builtin (e.g., 'echo'), then we get the expected behavior:

g() { echo >&2; }
v=$(g; echo foo; )
echo "v=$v"



>Fix:
	

>Unformatted:
 	
 	


Home | Main Index | Thread Index | Old Index