0

I am working on an program related to shell commands.

I want to make that, when the user enter an command, this not show any output.

I have tried adding > /dev/null to the command, this works well for commands like

$ echo "some text" | sudo tee some.txt  > /dev/zero

$ cat some.txt
some text

But if the user enter a command with append (>>), the new line is not added as expected

$ echo "appending another line" >> some.txt > /dev/zero

$ cat some.txt
some text

I understood that the commands were executed from left to right, something escapes me

Bruno Lima
  • 25
  • 4
  • 1
    A program only has one set of file descriptors when it starts up. So when `sudo tee` is invoked, its stdout can go to one place. It can go to `some.txt`, **or** it can go to `/dev/zero`, but it's strictly one or the other. While you're doing two redirections, and they are indeed left-to-right, but only the last of those redirections (the rightmost one!) is the one that's still in place when `sudo` is executed. – Charles Duffy Jun 17 '22 at 22:09
  • 1
    BTW, note that "left to right" is _processing order_, not necessarily execution. All parts of a pipeline run in parallel to each other, and there's no guarantee of startup order. – Charles Duffy Jun 17 '22 at 22:11

1 Answers1

1

if the user enter a command with append (>>), the new line is not added as expected

The redirections, are executed left to right. >>some.txt redirects stdout to some.txt, then >/dev/null overwrites that redirection and redirects stdout to /dev/null. Then the command is executed and the output of echo command is redirected to /dev/null.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111