After reading explanations of how the exec builtin works in bash, I understand that its basic function is to replace the current process without forking. It also seems to be used for redirecting I/O and closing file descriptors in the current process, which confuses me. Is this some unrelated additional thing exec
does? Can it be understood in the context of "replacing the current process"? And how does this work when combined with process substitution, e.g. exec 3< <(my program)
?
Here's what exec
does:
open
, dup2
and close
syscalls for most operations like > foo
pipe
+ fork
+ /dev/fd/*
is used for process substitionexecve
) with the specified program, if anyIf you don't specify a program to run, step 2 is simply skipped, and all redirections therefore affect the rest of the script.
<(Process substitution)
works by pipe
+fork
+/dev/fd/
:
/dev/fd/63
, a special file that will return FD 63 when opened. (try echo <(ls)
).From then on, it works just like redirecting from any other file. You open /dev/fd/63
for reading on FD 3, and then you end up reading from the pipe. exec
therefore doesn't need to do anything special.
subst.c
and the function is process_substitute
, there are quite a few comments. Line 5049 here: code.metager.de/source/xref/gnu/bash/subst.c — Jan 12, 2017 at 09:20 Local articles referenced by this article: