Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell builtin with redirection

Tags:

shell

unix

We are working on a shell (school project). We don't understand a behaviour. Why builtins are not acting when redirected ?

like

cd - | command

does not change the directory.

or

export NAME=VALUE | command

does not create the variable.

Thank you.

like image 535
Quentin Le Bévillon Avatar asked Jun 17 '26 06:06

Quentin Le Bévillon


1 Answers

Links of a pipeline are run in forked subshells.

In bash, you can print the PID of the current process with $BASHPID, so something like:

self(){ echo $BASHPID; } ; self ; self >&2 | self; self

should give you something like:

12849
12851
12852
12849

with the middle two PIDs being different than the first and the last (the mother shell) (in some shells, the first or the last link is run in the mother shell, but not in bash).

Changing the current directory or exporting a value in a subshell won't affect the parent shell in any way whatsoever.

like image 163
PSkocik Avatar answered Jun 19 '26 02:06

PSkocik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!