Bill Ricker <[email protected]> writes:
> Answer - ​It's even harder to find an actual Bell Unix (Bourne) 'sh' shell
> today than an authentic Berkeley Csh.  Most 'sh' are actually Bour​ne Again
> SHell (BASH) or later Gnu-ish/Linux derivatives there-of e.g. DASH, which
> incorporate a lot of CSH-isms.
>
> Ubuntu 14.04 DASH
> $ ls nonesuch >& /dev/null
> $ ls nonesuch >& err.1
> $ cat err.1
> ls: cannot access nonesuch: No such file or directory
> $
>
> It works despite what we 'know' from System V man(1).

To make things more confusing, here's sh as a mode of pdksh (OpenBSD):

$ /bin/sh
$ ls nonesuch >& err.1
/bin/sh: >&err.1 : illegal file descriptor name
$ ls nonesuch >&1 > err.1     # no-op
ls: nonesuch: No such file or directory
$ ls -s err.1
0 err.1
$ ls nonesuch 2>&1 > err.1    # no-op ?
ls: nonesuch: No such file or directory
$ ls -s err.1
0 err.1
$ ls nonesuch 2>&1 | cat > err.1   # or not...
$ cat err.1   
ls: nonesuch: No such file or directory
$ ( ls  nonesuch 2>&1 ) > err.1
$ cat err.1
ls: nonesuch: No such file or directory
$ ls nonesuch > err.1 2>&1 
$ cat err.1
ls: nonesuch: No such file or directory

Help from the ksh man page:

     Redirections are processed after pipelines are created and in the
     order they are given, so the following will print an error with a
     line number prepended to it:

           $ cat /foo/bar 2>&1 > /dev/null | cat -n

It was still confusing to me, reading that, but thinking of which file
each descriptor refers to as each redir operator is applied by the
shell, in the order it applies them, makes it clear:

a)   |      1 = pipe, 2 = terminal
b)  2>&1    1 = pipe, 2 = pipe
c)   >      1 = /dev/null, 2 = pipe

Contrast with the following:

$ cat /foo/bar > /dev/null  2>&1 | cat -n

a)   |      1 = pipe, 2 = terminal
b)   >      1 = /dev/null, 2 = terminal
c)  2>&1    1 = /dev/null, 2 = /dev/null


But I know I've been over this before and probably will only remember
it as, "when in doubt stick 2>&1 towards the end."


_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to