/dev/null 2>&1
&1 >/dev/null
For a long time, I'm confused on the subtle difference between the following two usages:
/dev/null 2>&1
&1 >/dev/null
Could you please give me an example to present the difference?
/dev/null 2>&1first directs stdout to /dev/null,
/dev/null 2>&1will result in both stdout and stderr directing to /dev/null
&1 >/dev/nullfirst directs stderr to whatever stdout is currently directed to,
&1 >/dev/nullwill result in both stderr being directed to /tmp/file1, and
On Fri, 19 May 2023 16:51:47 -0700, [email protected] wrote:
For a long time, I'm confused on the subtle difference between the following two usages:
/dev/null 2>&1
&1 >/dev/null
Could you please give me an example to present the difference?The redirection
/dev/null 2>&1first 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>&1will result in both stdout and stderr directing to /dev/null
On the other hand, he redirection
&1 >/dev/nullfirst 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
&1 >/dev/nullwill result in both stderr being directed to /tmp/file1, and
stdout being directed to /dev/null
/dev/null 2>&1
/dev/null 1>&2
HTH
--
Lew Pitcher
"In Skills We Trust"
On Saturday, May 20, 2023 at 8:27:32 AM UTC+8, Lew Pitcher wrote:
On Fri, 19 May 2023 16:51:47 -0700, [email protected] wrote:
For a long time, I'm confused on the subtle difference between the following two usages:The redirection
/dev/null 2>&1
&1 >/dev/null
Could you please give me an example to present the difference?
/dev/null 2>&1first 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>&1will result in both stdout and stderr directing to /dev/null
On the other hand, he redirection
&1 >/dev/nullfirst 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
&1 >/dev/nullwill result in both stderr being directed to /tmp/file1, and
stdout being directed to /dev/null
Thank you for your detailed explanation. If so, the following two seem equivalent:
/dev/null 2>&1
/dev/null 1>&2
For simplicity, the first should be used.
HTH
--
Lew Pitcher
"In Skills We Trust"
Zhao
For a long time, I'm confused on the subtle difference between
the following two usages:
/dev/null 2>&1
&1 >/dev/null
Could you please give me an example to present the difference?
"[email protected]" <[email protected]>:
For a long time, I'm confused on the subtle difference between
the following two usages:
/dev/null 2>&1
&1 >/dev/null
Could you please give me an example to present the difference?
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 <[email protected]>
Newsgroups: comp.unix.shell
Date: Tue, 05 Oct 2021 21:55:27 +0200
Message-ID: <[email protected]>
References: <[email protected]>
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.
Also <http://al.howardknight.net/?STYPE=msgid&A=0&MSGI=%3C835yubi6vk.fsf%40helmutwaitzmann.news.arcor.de%3E>
might help you.
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.
For a long time, I'm confused on the subtle difference between the following two usages:
/dev/null 2>&1
&1 >/dev/null
Could you please give me an example to present the difference?
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 135:12:33 |
| Calls: | 12,087 |
| Files: | 14,997 |
| Messages: | 6,517,362 |