Aman
Aman

Reputation: 1197

stdout & stderr of bunch of lines to /dev/null

Sorry, if the question has been asked previously, or is simple. I googled a bit and couldn't find the answer, and I a novice.

I usually use &>/dev/null to redirect stdout and stderr for each command that I have to. In one of my codes, I have to do this for 10 consecutive commands which is ugly :)

   Command 1 &>/dev/null
   Command 2 &>/dev/null
   .
   .
   .
   Command 10 &>/dev/null

Is there anyway to do this procedure for all of them at once; for example

   Command 1 
   Command 2 
   .
   .
   .
   Command 10 
   **Redirect all of them together**

Thanks :)

Upvotes: 2

Views: 157

Answers (3)

glenn jackman
glenn jackman

Reputation: 247062

I like @BroSlow's answer best. Another way to redirect stdout and stderr

echo before

# turn off stdout and stderr
# (but save their currect locations first)
exec 3>&1 1>/dev/null
exec 4>&2 2>/dev/null

echo no
echo error >&2
echo output

# restore stdout and stderr
# and close the temp file descriptors
exec 1>&3 3>&-
exec 2>&4 4>&-

echo after
echo after error >&2

You'll see the "before" and "after" stuff, but not the output in the middle.

Upvotes: 2

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11603

Just surround them with braces

{
  Command 1
  Command 2
  .
  .
  .
  Command 10
} &>/dev/null

Upvotes: 4

BMW
BMW

Reputation: 45313

Not sure the number after command is the parameter for command or file descriptor numbers? Can you explain?

#!/usr/bin/env bash
for i in {1..10}
do
  command $i &>/dev/null
done

Upvotes: 0

Related Questions