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

前端 未结 8 1749
慢半拍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:12

    Just FYI; in ksh it is working as expected; See also http://kornshell.com/doc/faq.html, Section III (shell programming questions), Q13:

    Q13.    What is $bar after, echo foo | read bar?
    A13.    The is foo.  ksh runs the last component of a pipeline
            in the current process.  Some shells run it as a subshell
            as if you had invoked it as  echo foo | (read bar).
    

提交回复
热议问题