1

I am learning the UNIX environment and have a question.. When it comes to redirecting a std error to a file, what happens if the file we redirect to has writing permissions denied to the user?

Example:

./command >file 2>&1
./command 2>&1 >file

What happens in the first instance to the error message? Does it automatically get discarded? I get the same error for both lines, that permission is denied but since the system reads from left to right, whats the difference between the way it's handled?

Any links to tutorials that go into the mechanism behind this would be helpful, so far I can only find information on how it handles it if we have writing permissions.

  • 1
    How about just trying it? $ touch out1; $ chmod 000 out1; $echo "Hello there" > out1; bash: out1: Permission denied – MariusMatutiae Mar 7 '17 at 12:49
1

Short answer: the command won't execute at all.

Explanation: the redirection is based on file descriptors, which needs to be created with the "open()" call, before executing the process.

My bash simply rejected it:

$ sleep 1 2>&1 > /nonwritable
-bash: /nonwritable: Permission denied

So you see, there is a safeguard for most shells.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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