What Does "< /Dev/Null >& /Dev/Null" at the End of a Command Do

What does /dev/null mean at the end of shell commands

2> means "redirect standard-error" to the given file.

/dev/null is the null file. Anything written to it is discarded.

Together they mean "throw away any error messages".

Why do we do ' /dev/null' in certain shell commands

The purpose of < /dev/null is to send EOF to a program immediately when tries to read from its standard input. It also means that the standard input is cut from receiving input from any other sources e.g. terminal.

The /dev/null is a special device in Unix which consumes everything written to it but signals EOF whenever commands tries to read from it without actually providing any actual data to read from.

The re-directions >/dev/null 2>&1 mean that redirect all standard output produced by the command to the null device which it consumes and redirect all error stream output(2) to the console.

Answering to another OP's question

So what happens if I don't send EOF to nc? I have try it and the script also worked

Nothing is expected to happen. By doing </dev/null we just signal the nc to not expect to read anything from standard input.


Another classic example of why you would need this is when using ssh if incorrectly used with a while loop. Consider an example, if you have a file servers.txt containing a list of server names and want to do an operation over ssh for all the hosts. You would do something like

while read -r server; do
ssh "$server" "uname -a"
done < servers.txt

You could think this being pretty straight forward with nothing expected to go wrong. But it does!

ssh will read from standard input. You have already added a read command with an input redirection over the file, so it also reads from standard input. The logic will be screwed up because after the first read command, the ssh will end up swallowing rest of the standard input which is undesired in your case.

So what do we do? We close the standard input of ssh by doing </dev/null as

ssh "$server" "uname -a" < /dev/null

What is /dev/null 2 &1?

>> /dev/null redirects standard output (stdout) to /dev/null, which discards it.

(The >> seems sort of superfluous, since >> means append while > means truncate and write, and either appending to or writing to /dev/null has the same net effect. I usually just use > for that reason.)

2>&1 redirects standard error (2) to standard output (1), which then discards it as well since standard output has already been redirected.

Exactly what is ` /dev/null ` when read is done by null?

For many systems, you can consult the manual page for special devices. For example, Linux null(4) documents:

Reads from /dev/null always return end of file

FreeBSD null(4) says it differently:

The null device accepts and reads data as any ordinary (and willing) file - but throws it away. The length of the null device is always zero.

which is less clear. Solaris null(7) says

Reads from a null special file always return 0 bytes.

You would see a redirect from /dev/null in a script when it is appeasing some program which wants to read from its standard input, but the script is not going to provide any data. If it were not redirected, the program might wait for keyboard input, for example.

What does - /dev/null mean in gcc -dM -E - /dev/null?

On its own, - means "read from standard input instead of the filename that would otherwise be provided on this command-line". This is a common Unix convention.

The < /dev/null redirects standard input from /dev/null, which is of length 0. Thus GCC will read from standard input and immediately reach the end of the input, causing it to print only the predefined macros (and not any macros in input, because there isn't any input). This is standard shell syntax, not specific to invocation of GCC.

Together, they are a way to provide no input to a process that expects some.

How to check if a file exists in a shell script

You're missing a required space between the bracket and -e:

#!/bin/bash
if [ -e x.txt ]
then
echo "ok"
else
echo "nok"
fi

How does /dev/null eat up output streams?

>/dev/null redirects the command standard output to the null device, which is a special device which discards the information written to it.
It's all implemented via file_operations (drivers/char/mem.c if you're curious to look yourself):

static const struct file_operations null_fops = {
.llseek = null_lseek,
.read = read_null,
.write = write_null,
.splice_write = splice_write_null,
};

write_null is what's called when you write to /dev/null. It always returns the same number of bytes that you write to it:

static ssize_t write_null(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
return count;
}

That's it. The buffer is just ignored.

Send terminal output of software to file or /dev/null

OP confirmed that my proposal in a comment worked, even without knowing why.

You can redirect output to 'normal' files with

velvet -option1 -option2 -file1 -file2 > /tmp/velvet1.out 2>/tmp/velvet2.out


Related Topics



Leave a reply



Submit