Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect output from dd command to /dev/null?

Tags:

linux

shell

dd

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!

like image 232
webminal.org Avatar asked Apr 07 '10 09:04

webminal.org


People also ask

How do I redirect the output to Dev Null?

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.

What does dd if =/ dev zero of =/ dev null do?

/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.

What is 2 >/ dev null in bash?

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.

What is Dev Null in shell script?

/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.


2 Answers

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
like image 141
pixelbeat Avatar answered Nov 01 '22 14:11

pixelbeat


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
like image 27
codaddict Avatar answered Nov 01 '22 12:11

codaddict