1

In shell scripting, are any file descriptors other than 0, 1 and 2 special in any way?

The flock manpage has the following example code:

  (
    flock -n 9 || exit 1
         # ... commands executed under lock ...
  ) 9>/var/lock/mylockfile

Why choose 9. Why not go for 3?

3

The 9 in flock(1) is just an example (the author of the man page might have chosen 42 or many other numbers). Some shell scripts get executed in some weird contexts, in which file descriptor 3 might be already used.

(Not every script is for interactive use, some might be started internally by server daemons - e.g. mail or web servers -, others might be started by some other command; some scripts are sitting in /usr/libexec -outside of usual $PATH - because they are for internal use only)

  • 3
    9 is the highest number that works in all shells. Many shells (e.g. Bourne, dash) don't parse something like 10>foo as a redirection from fd 10. – Gilles Aug 7 '15 at 22:29

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.