3 ms·
It seems to me that this can be achieved by the following bash-native way of creating extra file descriptor pipes: pipe_path="$(mktemp -u)" mkfifo "$pipe_p
by qazxcvbnm 3y ago
It seems to me that this can be achieved by the following bash-native way of creating extra file descriptor pipes:
pipe_path="$(mktemp -u)"
mkfifo "$pipe_path"
exec 3<>"$pipe_path"
rm -f "$pipe_path"
Here, exec associates the file descriptor (3 here, replace with any desired descriptor) with the pipe created by mkfifo. The filesystem path to the pipe is removed immediately after we obtain a file descriptor to it, so that the the only remaining reference to the pipe in the system would be from this script, and thus when the script dies, the kernel will automatically free the pipe.
An example use case would be like so: https://unix.stackexchange.com/a/216475/585293 https://unix.stackexchange.com/a/216475/585293
- pxeger1 3y agoWorth noting that POSIX sh only specifies that file descriptors up to 9 be supported, and many shells stick with this minimum.
- junon 3y agoMaximum, you mean? Or am I misunderstanding you? Written a lot of shell scripts in my days and somehow this fact escaped me. Thanks!
- js2 3y agoRephrased for clarity: POSIX specifies at least (a minimum of) 9. POSIX does not specify a maximum. Shells may provide more than 9. From the spec: > Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however, all implementations shall support at least 0 to 9, inclusive, for use by the application. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V... And from the bash manual: > Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally. https://www.gnu.org/software/bash/manual/bash.html#Redirections https://www.gnu.org/software/bash/manual/bash.html#Redirecti... Despite writing shell scripts for nearly three decades, I also was unaware of what POSIX had to say but I can't recall ever needing more than a couple extra FDs at most.
- junon 3y agoAh, thanks! Makes more sense :)