Bash Exec Command – What Does ‘exec {fd}

bashexecio-redirection

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

Best Answer

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