Subject: Re: Do test-path_is_{file,dir,exists} make sense anymore with -x?

From: SZEDER Gábor <[email-masked]>
Date: Mon, 4 Mar 2019 15:36:33 +0100

On Tue, Feb 26, 2019 at 04:01:01PM -0500, Jeff King wrote:
> > +	{ set +x ; } 2>/dev/null 4>/dev/null
> 
> Ah, this is the magic. Doing:
> 
>   set +x 2>/dev/null
> 
> will still show it, but doing the redirection in a wrapping block means
> that it is applied before the command inside the block is run. Clever.

Yeah, clever, but unfortunately (and to me suprisingly) unportable:

  $ ksh
  $ set -x
  $ echo foo
  + echo foo
  foo
  $ set +x
  $ 

It doesn't show 'set +x', how convenient! :)
However:

  $ set -x
  $ echo foo 2>/dev/null
  + echo foo
  + 2> /dev/null
  foo
  $ { set +x; } 2>/dev/null
  + 2> /dev/null
  $ 

Apparently ksh, ksh93 and mksh show not only the executed commands
but any redirections as well.  It's already visible when running our
tests with ksh and '-x':

  $ ksh ./t9999-test.sh -x
  Initialized empty Git repository in /home/szeder/src/git/t/trash directory.t9999-test/.git/
  expecting success: 
          true
  
  + true
  + 2> /dev/null ok 1 - first
  
  # passed all 1 test(s)
  1..1

NetBSD's sh:

  # set -x
  # echo foo
  + echo foo
  foo
  # echo foo 2>/dev/null
  + echo foo 2>/dev/null
  foo
  # { set +x; } 2>/dev/null
  + using redirections: 2>/dev/null do