I have a shell function
func() {
read < /dev/ttyUSB3 &
PID=$!
echo "$PID"
}
do not focus on its exact purpose, the thing is that I need to run some app (in this case read
) in background, and get its pid to kill in the future.
The problem I get is, in calling script
MYVAR=$(func)
is hung. I suspect it waits for termination of the background process, but why?
Simpler deal:
echo $(read < /dev/ttyUSB3 &)
also hangs, ^C does not work anymore, ^Z too.
I would expect echo to return empty string, as read starts, goes into background and says nothing into the stdout. When echo ends, that read process will become zombie.
Something is obviously broken in my logic. What is it?
How do I run input/output waiting application into background letting it do what it needs to, getting its PID without further problems like this?