57
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 the file null. If we remove 2, then the output is in the file instead of the variable. Any idea what exactly this line of code doing?

DreamBold
  • 2,727
  • 1
  • 9
  • 24
Smith
  • 607
  • 1
  • 5
  • 12
  • 1
    `${script} $* >> $logfile 2>&1 < /dev/null` What does `2>&1 <' represents here? Answer: [In the shell, what is “ 2>&1 ”?](http://stackoverflow.com/questions/818255/in-the-shell-what-is-21) – Smith Oct 03 '13 at 06:05
  • I voted to reopen. "What is `2>`" is a specific programming question, the question gives an example, and provides context. – stevec Mar 21 '23 at 04:32

3 Answers3

58

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
8

any idea what exactly the '2' is doing over here

Here 2 is a file descriptor referring to STDERR.

2> /dev/null implies that STDERR be redirected to the null device /dev/null.

The complete line you've mentioned stores the output, i.e. STDOUT (ignoring the STDERR), returned by ls $directory_/fallback_* into the variable scriptlist.

devnull
  • 118,548
  • 33
  • 236
  • 227
6
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).

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59