What does `1>>` and `2>>` mean in a bash script?

I have the following bash script, from what I understand >> is used to append the output of a command to an existing file instead of overwrite, but what is it doing in this case? This script is calling some exe files to convert from one format to another. There are many years for each file, so it loops through each file by looking at the filename. Also when I run this script I get “ambiguous redirect”

source $HOME/.bashrc


# Set some paths and prefixes



while [ ${yr} -le ${yr_end} ]
   ./executable1 ${pth_data}file${yr}-${yr}.nc ${yr} ${pth_rst} 1>> ${jobout} 2>> ${joberr}
   ./executable2 ${pth_data}file${yr}-${yr}.nc ${yr} ${pth_rst} 1>> ${jobout} 2>> ${joberr}
   ./executable3 ${pth_data}file${yr}-${yr}.nc ${yr} ${pth_rst} 1>> ${jobout} 2>> ${joberr}
   let yr=${yr}+1
Asked By: Herman Toothrot


1>> and 2>> are redirections for specific file-descriptors, in this case the standard output (file descriptor 1) and standard error (file descriptor 2).

So the script is redirecting all “standard” messages to ${jobout} and all error messages to ${joberr}. Using >> in both cases means all messages are appended to the respective files.

Note that ${jobout} and ${joberr} take their values from the two command-line parameters to the script (${1}and ${2}), so you need to specify the files you want to use to store the messages. If the parameters aren’t given the script will produce the “ambiguous redirection” error message you’ve seen; the script should really check whether the parameters have been provided and produce an appropriate error message otherwise, something like

if [ -z "$1" -o -z "$2" ]; then
    echo "Log files for standard and error messages must be specified"
    echo "${0} msgfile errfile"
    exit 1

at the start of the script.

Answered By: Stephen Kitt

In your case 1>> append the information from current stdout handler to the file ${jobout}
2>> append the information from current stderr handler to the file ${joberr}

jobout and joberr are files, defined as first and second parameter of script

Answered By: Romeo Ninov

As there are no parameters in calling the script the parameters $1 and $2 are empty and so are $jobout and $joberr.

You must call the script like this:

./myscript.sh file1 file2
Answered By: Hauke Laging
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.