在子 shell 中使用 read 内置命令从父标准输入读取

Use read builtin command to read from parent stdin while in a subshell

我有脚本启动 subshell/background 命令来读取输入,然后做更多的工作:

#!/bin/bash

(
while true; do
    read -u 0 -r -e -p "test_rl> " line || break
    echo "line: ${line}"
done
) &

sleep 3600 # more work

使用上面的方法我什至没有得到提示。如果我在启动子 shell 之前 exec 3>&0 然后从描述符 3 (-u 3) 读取,那么我至少会得到提示,但是读取命令仍然没有得到我键入的任何输入。

如何让读取内置函数从终端(父级的标准输入文件描述符)正确读取?

How do I get the read builtin to read correctly from the terminal (parent's stdin file descriptor)?

您可能想试试这个(使用父文件描述符):

#!/bin/bash

(
while true; do
    read -u 0 -r -e -p "test_rl> " line || break
    echo "line: ${line}"
done
)<&0 >&1 &

sleep 3600 # more work