In POSIX sh, how to preserve variables set in a 'while read' loop when the input is continuous (event watcher)? In POSIX sh, how to preserve variables set in a 'while read' loop when the input is continuous (event watcher)? shell shell

In POSIX sh, how to preserve variables set in a 'while read' loop when the input is continuous (event watcher)?


The named pipe is the solution, but you are using it in the wrong place.

NAMED_PIPE="/tmp/mypipe"mkfifo "$NAMED_PIPE"command > "$NAMED_PIPE" &while read -r foo bar; do    MY_VAR="$foo"    echo "$MY_VAR"done < "$NAMED_PIPE"echo "$MY_VAR"