27

That is literal, {fd} isn't a placeholder. I have a script that does this, and does not source in anything, nor does it reference {fd} anywhere else. Is this valid bash?

exec {fd}</dev/watchdog

33

Rather than having to pick a file descriptor and hope it's available:

exec 4< /dev/watchdog  # Was 4 in use? Who knows?

this notation asks the shell to pick a file descriptor that isn't currently in use, open the file for reading on that descriptor, and assign the number to the given variable (fd).

$ exec {fd}< /dev/watchdog
$ echo $fd
10
  • I'm sure that you're right, because (1) I recall reading it somewhere, and (2) I just tried it (and it works).  But I couldn't find it documented anywhere.  (Of course, I had less than ten minutes to search before you posted your answer.)  Can you provide a reference to where it is documented? – Scott Aug 28 '15 at 20:01
  • 2
    See the man page, 2nd paragraph of the section REDIRECTIONS. It doesn't really jump out at you, since they don't give an explicit example. – chepner Aug 28 '15 at 20:04
  • You are right, I missed it in man bash, but it's there where you said. It should be noted that any word, not just fd will work in the braces, and that it assigns an fd >= 10. – Gregg Leventhal Aug 28 '15 at 20:27
  • 3
    @Scott, not only is it only in bash, but it's only in bash 4.1 or newer (thus not available on the 3.2 releases still used on MacOS). – Charles Duffy Aug 28 '15 at 22:03
  • 2
    @CharlesDuffy /dev/watchdog doesn't exist on Mac OS X either, though; it's a Linux-specific device. – duskwuff Aug 28 '15 at 22:32
11

The form:

{var}<filename

made the shell open file filename for reading and store file descriptor number in variable var. There's no space allowed between {var} and redirection operators, and the file descriptor number will be greater than or equal 10.

This feature was original from ksh (from version ksh93r in 2006), bash copied it a lot later in bash-4.1-alpha from 2010. zsh also made the change earlier than bash from zsh 4.3.4 in 2007.

2

This is explained in the Bash manual section on Redirections.

Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}. In this case, for each redirection operator except >&- and <&-, the shell will allocate a file descriptor greater than 10 and assign it to {varname}. If >&- or <&- is preceded by {varname}, the value of varname defines the file descriptor to close.

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.