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

    From [email protected]@21:1/5 to All on Fri May 19 16:51:47 2023
    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?

    Regards,
    Zhao

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to [email protected] on Sat May 20 00:25:55 2023
    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>&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
    &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
    &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"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to Lew Pitcher on Fri May 19 17:46:26 2023
    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:

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

    Could you please give me an example to present the difference?
    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
    &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
    &1 >/dev/null
    will 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to [email protected] on Sat May 20 01:18:07 2023
    On Fri, 19 May 2023 17:46:26 -0700, [email protected] wrote:

    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:

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

    Could you please give me an example to present the difference?
    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
    &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
    &1 >/dev/null
    will 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

    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

    HTH
    --
    Lew Pitcher
    "In Skills We Trust"

    Zhao




    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Waitzmann@21:1/5 to All on Sat May 20 03:35:49 2023
    "[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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to Helmut Waitzmann on Fri May 19 19:58:49 2023
    On Saturday, May 20, 2023 at 9:48:14 AM UTC+8, Helmut Waitzmann wrote:
    "[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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Spiros Bousbouras@21:1/5 to [email protected] on Sat May 20 05:13:36 2023
    On Fri, 19 May 2023 19:58:49 -0700 (PDT)
    "[email protected]" <[email protected]> wrote:
    Also <http://al.howardknight.net/?STYPE=msgid&A=0&MSGI=%3C835yubi6vk.fsf%40helmutwaitzmann.news.arcor.de%3E>
    might help you.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gerg@21:1/5 to [email protected] on Sun May 21 02:11:23 2023
    In article <[email protected]>, [email protected] <[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?


    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 ::::: [email protected] :::::::::::::
    I have a map of the United States that's actual size.
    -- Steven Wright

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)