Ubuntu – Spaces in commands with redirection

bashcommand lineredirect

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.

Best Answer

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:

metacharacter

A character that, when unquoted, separates words. One of the following:

|  & ; ( ) < > space tab

And

word A sequence of characters considered as a single unit by the shell. Also known as a token.

So when we do:

$ echo "hello world">/dev/null

or

$ echo "hello world" > /dev/null

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:

$ stat ./non-existent file 2>&1  > /dev/null
stat: cannot stat './non-existent': No such file or directory
stat: cannot stat 'file': No such file or directory

You're making file descriptor 2 output to same location as 1, 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 where 2 points - then it'll work:

$ stat ./non-existent file > /dev/null 2>&1