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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityRather 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
REDIRECTIONS
. It doesn't really jump out at you, since they don't give an explicit example.
/dev/watchdog
doesn't exist on Mac OS X either, though; it's a Linux-specific device.
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.
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.