How to redirect output from dd command to /dev/null?
Asked Answered
R

2

6

In shell script i need to redirect output from dd command to /dev/null - how to do that?

( dd if=/dev/zero of=1.txt count=1 ) 2>&1 /dev/null

didn't work!

Robert answered 7/4, 2010 at 9:25 Comment(0)
H
5

If you want to redirect only the standard output of the command do:

( dd if=/dev/zero of=1.txt count=1 ) > /dev/null

and if you want to redirect both stdout and stderr to /dev/null do:

( dd if=/dev/zero of=1.txt count=1 ) > /dev/null 2>&1
Hendecahedron answered 7/4, 2010 at 9:27 Comment(2)
Specifically: You must first redirect stdout and then you can "copy" the new file descriptor to stderr.Lole
I just want to ignore messages from dd command -- messages should not be displayed . thanksRobert
S
6

No need for a subshell.

dd if=/dev/zero of=1.txt count=1 2>/dev/null

However what if there is an error? You could instead do:

err=$(dd if=/dev/zero of=1.txt count=1 2>&1) || echo "$err" >&2
Saenz answered 7/4, 2010 at 11:27 Comment(0)
H
5

If you want to redirect only the standard output of the command do:

( dd if=/dev/zero of=1.txt count=1 ) > /dev/null

and if you want to redirect both stdout and stderr to /dev/null do:

( dd if=/dev/zero of=1.txt count=1 ) > /dev/null 2>&1
Hendecahedron answered 7/4, 2010 at 9:27 Comment(2)
Specifically: You must first redirect stdout and then you can "copy" the new file descriptor to stderr.Lole
I just want to ignore messages from dd command -- messages should not be displayed . thanksRobert

© 2022 - 2024 — McMap. All rights reserved.