Linux sys vars?

Dale Farnsworth dale at farnsworth.org
Thu May 12 13:52:10 MST 2011


> Hi,
> 
> I keep seeing a reference to what looks like two pre-populated variables
> in shell scripts.  Actually this one is in a crontab.
> 
> Something like this :  /home/user-name/directory-name/scriptname.php
> >/dev/null 2>&1
> 
> What is the number 1 and 2?  Is there others?

Unix and copycats, like Linux use small non-negative integers to identify
files that may be read from or written to.  They are known as file
descriptors.  By convention, the first 3 file descriptors have a special
meaning.  0 is standard input, 1 is standard output, and 2 is standard
error.

Normally, each process inherits these 3 file descriptors from its
parent process but the shell allows you to "redirect" them to
other files.

Examples:
	echo hello >newfile	# Creates the file "newfile" and writes
				# "hello\n" to it.
	echo hello >/dev/null	# Writes "hello\n" to /dev/null, where it
				# disappears
	echo hello 2>newfile2	# Creates the file "newfile2" and puts
				# error messages from "echo hello" into it.
				# Since "echo hello" won't output any error
				# messages, "newfile2" will be an empty file.
	echo hello 2>&1		# Redirects the error output of the command
				# into the same file where the standard
				# output goes.

So, "scriptname.php >/dev/null 2>&1" says to redirect the standard output
of scriptname.php into the bit bucket and then to redirect the standard
error output to the same place.

-Dale


More information about the PLUG-discuss mailing list