Why is “echo foo | read a ; echo $a” not working as expected?

前端 未结 8 1753
慢半拍i
慢半拍i 2021-02-04 15:37

I could replicate the problem with various shells under FreeBSD, GNU/Linux, and Solaris. It had me head-scratching for more than an hour, so I decided to post the question here

8条回答
  •  一向
    一向 (楼主)
    2021-02-04 16:11

    According to Kernighan and Pike's The Unix Programming Environment (p. 159) "none of the shell built-in commands (as opposed to the control flow primitives, like for) can be redirected with > and <", and "this might be described as a bug in the shell". This seems to explain a) why code like

    ls *.c |
    while read file
    do
       echo $a
    done
    

    invariably works without a problem, and b) the inconsistency that redirecting from a file works.

提交回复
热议问题