简体   繁体   中英

why some bash commands with pipe doesn't work

I'm trying to implement my own shell, based on bash behavior, and i notice that there' some commands that doesn't work with pipes, like if i have: export AAA=test | cat export AAA=test | cat , for example this commands not going to add AAA to the environment variables, so it's like export didn't work,

but if i write: export | cat export | cat , it's going to print environment variable, so here, it's like export work, the same with exit, unset.... so can someone explain to me this behavior and how i can implement it?

export in both cases executes in a subshell , whose environment is a copy of the parent shell's.

With export | cat export | cat , you cat the contents of the subshell's environment, which wasn't modified from the copy received from the parent, so you get output that matches what export alone would have output.

With export AAA=test | cat export AAA=test | cat , you modify the environment of the subshell, not the calling shell. Further, export in this case doesn't write any output for cat to read. Once the pipe completes, the subshell is destroyed, and control reverts to the current shell, whose environment was not modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM