如果你能指出我在这里失败了,我会很高兴的:
line="some text"
printf "other text"|read line;printf '%s' "$line"
Run Code Online (Sandbox Code Playgroud)
输出:
一些文字
输出我的想法:
其他文字
这是次要的事情还是我错过了重要的事情?
由于管道,$line变量在子shell中分配,而父shell不记录更改.您可以使用该shopt -s lastpipe选项在当前shell中执行管道的最后一个命令
在此示例中,您只打印字符串,您也可以使用以下语法:
read line <<< "other text"; printf '%s' "$line"
Run Code Online (Sandbox Code Playgroud)
或者通常您可以使用流程替换
read line < <(printf "other text"); printf '%s' "$line"
Run Code Online (Sandbox Code Playgroud)