-2

I have a file hello.c with "hello world" program in C. I also done the following on the shell (UNIX):

$ ls > 1
$ chmod 0 1
$ cc -o hello hello.c
$ chmod 400 hello
$ ./hello > 1

And I got permission denied on 1.

Why I didn't get the permission denied error on hello?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Mickey
  • 480
  • 4
  • 13

1 Answers1

1

Because when you ask a shell to perform an output redirection for a command, the file to write to is opened by the shell, before invoking the command.

Your shell will:

  1. open 1 for writing
  2. "wire" ./hello stdout to 1
  3. run ./hello

1 has permissions 0o000, so the first operation will fail.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80