In bash, if you put:

ls /Users/*/.ssh/id_rsa 2>&1 > rsa-keys.log

...you're redirecting stderr to the stdout's destination while stdout is still sending output to the screen. So any permission errors encountered will go to the screen, not to rsa-keys.log.

From the bash manpage:

==================

Note that the order of redirections is significant. For example, the command

   ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

   ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist.

==================

Commands given to the shell are evaluated and processed in a specific order and fashion, and this is one quirk of that that many people are unaware of.

you are viewing a single comment's thread
view the rest of the comments
[–] 1 point 1 year ago (5 children)
  • [–] 1 point 1 year ago (4 children)

    https://www.man7.org/linux/man-pages/man1/bash.1.html

    Pipelines A pipeline is a sequence of one or more commands separated by one of the control operators | or |&. The format for a pipeline is:

    [time [-p]] [ ! ] command1 [ [|⎪|&] command2 ... ]

    (...) If |& is used, command1's standard error, in addition to its standard output, is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error to the standard output is performed after any redirections specified by command1.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 1 year ago* (3 children)

    So it is a bash thing.

    Just to be clear, because some seem to conflate Bash with Shell. If not specified, assume POSIX shell, that's how /bin/sh is handled as well. And that has no |&.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 1 point 1 year ago (2 children)

    True

    But nowadays /bin/sh is often just a link to bash

  • source
  • parent
  • hideshow 2 child comments