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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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.
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0