I'm reading an example bash shell script:

#!/bin/bash

# This script makes a backup of my home directory.

cd /home

# This creates the archive
tar cf /var/tmp/home_franky.tar franky > /dev/null 2>&1

# First remove the old bzip2 file.  Redirect errors because this generates some if the archive
# does not exist.  Then create a new compressed file.
rm /var/tmp/home_franky.tar.bz2 2> /dev/null
bzip2 /var/tmp/home_franky.tar

# Copy the file to another host - we have ssh keys for making this work without intervention.
scp /var/tmp/home_franky.tar.bz2 bordeaux:/opt/backup/franky > /dev/null 2>&1

# Create a timestamp in a logfile.
date >> /home/franky/log/home_backup.log
echo backup succeeded >> /home/franky/log/home_backup.log

I'm trying to understand the use of "/dev/null 2>&1" here. At first, I thought this script uses /dev/null in order to gracefully ignore errors, without causing the script to crash (kind of like try catch exception handling in programming languages). Because I don't see how using tar to compress a directory into a tar file could possibly cause any type of errors.

share|improve this question
3  
Redirecting to /dev/null won't prevent crashing, but will clean up the stdout and stderr output streams. tar could cause errors in a variety of ways. You might not have write access, the file might already exist, etc. – Sparhawk Mar 14 '14 at 4:27
2  
Just a trick to avoid unnecessary output. As for why tar can cause errors: because the target directory does not exist, because the source doesn't, because you don't have write access to the target, or read to the source, because tar is not in your $PATH, because tar crashed (you never know), because there's no space left on the device, because the tar version changed and now requires different syntax, because the disk caused an I/O error. I'm sure you could find more. – terdon Mar 14 '14 at 5:48
up vote 84 down vote accepted

No, this will not prevent the script from crashing. If any errors occur in the tar process (e.g.: permission denied, no such file or directory, ...) the script will still crash.

Because using > /dev/null 2>&1 will redirect all your command output (both stdout and stderr) to /dev/null, meaning no outputs are printed to terminal.

By default:

stdin  ==> fd 0
stdout ==> fd 1
stderr ==> fd 2

In the script, you use > /dev/null causing:

stdin  ==> fd 0
stdout ==> /dev/null
stderr ==> fd 2

And then 2>&1 causing:

stdin  ==> fd 0
stdout ==> /dev/null
stderr ==> stdout
share|improve this answer
    
As of understanding > /dev/null 2>&1 , this command result in stderr ==> stdout, so stderr still get printed to stdout?? – Weishi Zeng Apr 26 '16 at 7:10
    
@WeishiZeng: Yes, but at that time, stdout was pointed to /dev/null. – cuonglm Apr 26 '16 at 7:16
1  
why mention & before fd 1 (stdout) and not before fd 2 (stderr) ? – IamaTacos Jun 4 '16 at 21:28
2  
probably not too important, but what is fd? – kev Jul 28 '16 at 0:13
2  
@kev en.wikipedia.org/wiki/File_descriptor – bitek Sep 11 '16 at 10:56

I'm trying to understand the use of "> /dev/null 2>&1" here.

(note that I added the redirection before /dev/null in your question.)

The above would redirect the STDOUT and STDERR to /dev/null. It works by merging the STDERR into the STDOUT. (Essentially all the output from the command would be redirected to the null device.)

... without causing the script to crash (kind of like try catch exception handling in programming languages).

It's not quite like a try/catch or anything. It simply silences any sort of output (including error) from the command.

Because I don't see how using tar to compress a directory into a tar file could possibly cause any type of errors.

It could result in errors for a number of reasons, including:

  • Inadequate permissions on the file(s) you're attempting to archive or on the file that you're attempting to write to
  • Lack of disk space in order to create the archive
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.