Вопрос

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 :)

Это было полезно?

Решение 2

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.

Другие советы

Just surround them with braces

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

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top