program 2> error.log
program &> filename
program >> filename 2>&1
program 2>&1
I was able to figure out what these lines mean.
However I don't quite understand where I should put the spaces. I also worry that it actually doesn't matter where to put spaces. Thanks for reading.
Yes, spacing between words and redirections doesn't matter. That's the short answer.
The details lie in the fact that shell ( for simplicity let's just refer to
bash
only) treates certain characters and groups of characters as "words" and "metacharacters". From bash 4.3 manual:And
So when we do:
or
that's still 3 words ("hello world" can be considered a single shell word because it's quoted), with one
>
meta character and couple spaces. Shell will see it, and perform redirection first (so it looks for meta characters first), and then runs commands in accordance with its standard behavior.Order of redirections, however, matters a lot, especially when you're duplicating file descriptors with something like
2>&1
. Say you want to get send both stderr and stdin to same location. Here's a wrong way to do it:You're making file descriptor
2
output to same location as1
, which is your terminal, but it was already doing so. That's why stderr shows up.If you redirect
stdout
first, and only then change where2
points - then it'll work:Spacing doesn't matter except a few special cases.
According to the Bash manual:
So, for example,
echo test 2 > output.txt
will be interpreted asecho test 2 1> output.txt
, i.e. redirect the stdout of the commandecho test 2
to the file output.txt which will create the file containing the text "test 2".On the other hand,
echo test 2> output.txt
will be interpreted as "redirect the stderr (file descriptor 2) of the commandecho test
to the file output.txt", which will be empty because the stderr of the command is empty.This behavior is not limited to digits 1 and 2. So
echo test 5> output.txt
will also create an empty file.<(...)
or>(...)
, space between the left parentheses and the > or < is not allowed.