Descriptor lifetime and exec redirection
Without a command operand, exec applies redirections to the current shell; they remain until changed or closed. With a command operand, successful exec instead replaces the shell process (next chapter). Open a dedicated descriptor with exec 3> log, write with printf ... >&3, and close it using exec 3>&-. For input, use exec 3< file and exec 3<&-. Fixed descriptors can collide with existing handles; use a fresh shell in these examples. Bash can allocate an available descriptor of at least 10 using exec {fd}> log; the variable holds its number. Close that handle with exec {fd}>&-. Allocation without exec can also persist beyond a command when varredir_close is disabled; enabling that option normally closes such command-scoped handles. A variable containing the number is not the handle itself; unsetting the variable does not close it. After closure, the number can be reused by Bash or another open operation, so do not use a stale descriptor variable. Open descriptors can be inherited by child processes, so close unneeded ones before launching children; never rely on a child inheriting secrets safely. Saving a descriptor with exec 3>&1 permits restoring stdout later using exec 1>&3. Use a subshell when you want temporary shell-wide changes without restoring them manually. Check the status of descriptor setup before using it. In these disposable scripts, || exit 1 stops after failure; exiting also releases the process’s open descriptors. Closing a descriptor does not truncate or remove the file.
Practical examples
Run each example separately in a fresh Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Save and restore stdout
( exec 3>&1 || exit 1 exec > trace.log || exit 1 printf 'inside\n' || exit 1 exec 1>&3 || exit 1 exec 3>&- || exit 1 printf 'restored\n' || exit 1) &&cat trace.logExpected stdout:
restoredinsidePersistent redirection affects later commands inside the subshell until restoration.
Allocate a descriptor dynamically
exec {fd}> records.txt || exit 1printf 'alpha\n' >&"$fd" || exit 1printf 'beta\n' >&"$fd" || exit 1exec {fd}>&- || exit 1cat records.txtExpected stdout:
alphabetaThe number is deliberately not hardcoded; the named handle is closed.
Inheritance into a child Bash
exec 3> child.log || exit 1bash --noprofile --norc -c 'printf "child\n" >&3' || exit 1printf 'parent\n' >&3 || exit 1exec 3>&- || exit 1cat child.logExpected stdout:
childparentThe child inherits descriptor 3 in this fixture and writes into the same file.
Common mistakes
Do not close a descriptor you do not own. Do not run shell-wide exec redirections in your everyday interactive shell; test them in a new shell or subshell. Closing a descriptor does not delete the file.
Practice
In a fresh shell allocate an output descriptor dynamically, write two records, close it, and verify file contents. Repeat with saved stdout and restore it before printing a final message.
Next: Noclobber and redirection failures