Here is an example that starts two subprocesses implemented as functions of the same shell script ... One subprocess generates numbers 1 ... 5 (sleeps between fingerprints), the second one from a fixed filedescriptor (5, to which STDOUT of the first FD is redirected ), multiplied by 2 and prints again. The main process redirects the STDOUT of this second process to another fixed filedescriptor (6), and then to read from it in a loop.
It works basically the same as in C code with fd pairs created by the pipe (2) system call. To understand what is going on, run the script under strace -f!
Bash Version 4.2.24 (1) runs on Ubuntu / x86.
[ubuntu /home/chris] $ bash --version GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
Script output:
[ubuntu /home/chris] $ ./read_from_fd.sh Got number 2. Got number 4. Got number 6. Got number 8. Got number 10.
Source:
#!/bin/bash
Process tree during operation:
ββread_from_fd.sh(8118)ββ¬βread_from_fd.sh(8119)βββsleep(8123) ββread_from_fd.sh(8120)
source share