REVERSI
REVERSI

Reputation: 59

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, 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, 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 ?

Upvotes: 0

Views: 769

Answers (1)

chepner
chepner

Reputation: 530872

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

With 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, 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.

Upvotes: 4

Related Questions