Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

>/dev/null 2>&1 vs 2>&1 >/dev/null

27 views
Skip to first unread message

hongy...@gmail.com

unread,
May 19, 2023, 7:51:50 PM5/19/23
to
For a long time, I'm confused on the subtle difference between the following two usages:

>/dev/null 2>&1
2>&1 >/dev/null

Could you please give me an example to present the difference?

Regards,
Zhao

Lew Pitcher

unread,
May 19, 2023, 8:27:32 PM5/19/23
to
The redirection
>/dev/null 2>&1
first directs stdout to /dev/null,
then directs stderr to whatever stdout is currently directed to.

If, for instance, stdout starts out being directed to /tmp/file1
and stderr starts out being directed to /tmp/file2, then
>/dev/null 2>&1
will result in both stdout and stderr directing to /dev/null


On the other hand, he redirection
2>&1 >/dev/null
first directs stderr to whatever stdout is currently directed to,
then directs stdout to /dev/null.

If, for instance, stdout starts out being directed to /tmp/file1
and stderr starts out being directed to /tmp/file2, then
2>&1 >/dev/null
will result in both stderr being directed to /tmp/file1, and
stdout being directed to /dev/null


HTH
--
Lew Pitcher
"In Skills We Trust"

hongy...@gmail.com

unread,
May 19, 2023, 8:46:29 PM5/19/23
to
Thank you for your detailed explanation. If so, the following two seem equivalent:

>/dev/null 2>&1
2>/dev/null 1>&2

For simplicity, the first should be used.

> HTH
> --
> Lew Pitcher
> "In Skills We Trust"

Zhao

Lew Pitcher

unread,
May 19, 2023, 9:18:13 PM5/19/23
to
Yes, they both result in stdout and stderr being directed to /dev/null

> For simplicity, the first should be used.

Yes. The common idiom is >/dev/null 2>&1

Helmut Waitzmann

unread,
May 19, 2023, 9:48:14 PM5/19/23
to
"hongy...@gmail.com" <hongy...@gmail.com>:
Do you have access to news articles back in 2021?  Then you might
take a look at the article

Subject: Redirecting file descriptors (was: How to redirect
to non-standard file descriptors?)
From: Helmut Waitzmann <nn.th...@xoxy.net>
Newsgroups: comp.unix.shell
Date: Tue, 05 Oct 2021 21:55:27 +0200
Message-ID: <835yubi...@helmutwaitzmann.news.arcor.de>
References: <irttei...@mid.individual.net>

It is a followup to an article posted by Robert Latest, who asked
a similar question as you do.

If you (or any other person) don't have access to it any more, I
could either send it to you by e‐mail or repost it here.

Also
<http://al.howardknight.net/?STYPE=msgid&A=0&MSGI=%3C835yubi6vk.fsf%40helmutwaitzmann.news.arcor.de%3E>
might help you.

hongy...@gmail.com

unread,
May 19, 2023, 10:58:52 PM5/19/23
to
I can open the above URL, but the message shown there includes some non-human readable stuff, for example:

the shell first will do the same and then tell the kernel by means=20
of the system service =E2=80=9Cdup2=E2=80=9D to copy the contents of the en=
try #1 to=20
the entry #2.=C2=A0 Now two entries in the process' file descriptor=20
table=C2=A0=E2=80=93 #1 and #2=C2=A0=E2=80=93 refer to the opened file =E2=
=80=9Coutput.txt=E2=80=9D.

Best,
Zhao

Spiros Bousbouras

unread,
May 20, 2023, 1:16:07 AM5/20/23
to
More simply http://al.howardknight.net/?ID=165151671300 .I have that
whole thread saved , very instructive.

> I can open the above URL, but the message shown there includes some
> non-human readable stuff, for example:
>
> the shell first will do the same and then tell the kernel by means=20
> of the system service =E2=80=9Cdup2=E2=80=9D to copy the contents of the en=
> try #1 to=20
> the entry #2.=C2=A0 Now two entries in the process' file descriptor=20
> table=C2=A0=E2=80=93 #1 and #2=C2=A0=E2=80=93 refer to the opened file =E2=
> =80=9Coutput.txt=E2=80=9D.

quoted-printable strikes again. al.howardknight.net doesn't do any
decoding of the articles. =E2=80=9C means the 3 octets with hexadecimal
values E2 , 80 , 9C which correspond to a certain quote character (not
the familiar ASCII quotes) in UTF-8 encoding.

gerg

unread,
May 20, 2023, 10:12:33 PM5/20/23
to
In article <e09ff1ac-02e0-49dc...@googlegroups.com>,
There are also documents on the Internet that go into detail about
these sort of things. For example:

<https://mywiki.wooledge.org/BashGuide/InputAndOutput#line-327>

(scrolling up a little bit on the page also gives useful info)

--
::::::::::::: Greg Andrews ::::: ge...@panix.com :::::::::::::
I have a map of the United States that's actual size.
-- Steven Wright
0 new messages