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!
In Unix, how do I redirect error messages to /dev/null? You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.
/dev/zero provides an endless stream of zero bytes when read. This function is provided by the kernel and does not require allocating memory. All writes to /dev/null are dropped silently. As a result, when you perform the dd , the system generates 500 megabytes in zero bytes that simply get discarded.
by Zeeman Memon. Whether you are a new Linux user or an experienced bash programmer, it is highly probable that you encountered the cryptic command 2>/dev/null. Although this command looks technically complex, its purpose is very simple. It refers to a null device that is used to suppress outputs of various commands.
/dev/null in Linux is a null device file. This will discard anything written to it, and will return EOF on reading. This is a command-line hack that acts as a vacuum, that sucks anything thrown to it.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With