Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 2> mean on the Unix command-line? [closed]

scriptlist=`ls $directory_/fallback_* 2> /dev/null` 

What exactly is the purpose of the 2> part of the command? I omitted it and ran the command, it just works fine.

And, if the output of ls is getting stored in /dev/null file, what exactly the variable scriptlist will contain. When I executed the code, the output was in the variable and nothing was there in file null. If we remove 2, then output is in file instead of variable. Any idea what exactly this line of code doing?

like image 288
Smith Avatar asked Oct 01 '13 05:10

Smith


People also ask

What does 2 &1 at the end of a command do?

The 1 denotes standard output (stdout). The 2 denotes standard error (stderr). So 2>&1 says to send standard error to where ever standard output is being redirected as well.

What does 2 >> mean in Unix?

(other special file descriptors include 0 for standard input and 1 for standard output). 2> /dev/null means to redirect standard error to /dev/null . /dev/null is a special device that discards everything that is written to it.

What does 2 mean in Linux?

The Linux operating system refers to each of these files with a unique non-negative integer. '0' for standard input. '1' for standard output. '2' for standard error.

What does 2 >& 1 mean in shell script?

1 "Standard output" output file descriptor. The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").


2 Answers

File descriptor 2 represents standard error. (other special file descriptors include 0 for standard input and 1 for standard output).

2> /dev/null means to redirect standard error to /dev/null. /dev/null is a special device that discards everything that is written to it.

Putting all together, this line of code stores the standard output of command ls $directory_/fallback_* 2> /dev/null into the variable scriptlist, and the standard error is discarded.

like image 186
Yu Hao Avatar answered Sep 21 '22 02:09

Yu Hao


scriptlist=`ls $directory_/fallback_* 2> /dev/null` 

As you have enclosed the whole line ls $directory_/fallback_* 2> /dev/null in backticks, the output of the ls command is stored in scriptlist variable.

Also, the 2> is for redirecting the output of stderr to /dev/null (nowhere).

like image 34
Suvarna Pattayil Avatar answered Sep 20 '22 02:09

Suvarna Pattayil