P
P
Pavel Kulaga2020-02-04 13:11:36
*nix-like systems
Pavel Kulaga, 2020-02-04 13:11:36

"cd | ls -R /" Does the shell create two processes with fork or just one for ls?

Actually, I can’t understand this ...
Here I was looking for a mention of how built-in should be called according to the standard, but I did not find it.
The question arose after jobs showed that cd had a pid:

[1]  - 50095 done       echo |
       50096 suspended  ls -R /
[2]  + 50659 done       cd |
       50660 suspended  ls -R /

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-02-04
@hapr-lera

It's so easy to find out, see:

$ strace -f -e clone,write,chdir bash -c "cd | echo 'zz'"
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f1c61235a10) = 7494
strace: Process 7494 attached
[pid  7493] clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f1c61235a10) = 7495
strace: Process 7495 attached
[pid  7494] chdir("/home/jcmvbkbc")     = 0
[pid  7495] write(1, "zz\n", 3zz
)         = 3
[pid  7494] +++ exited with 0 +++
[pid  7495] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=7494, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++

strace started bash (7493), bash created two processes, one of them (7494) executed cd, the second (7495) executed echo.

K
Karpion, 2020-02-04
@Karpion

As I understand it, cd must be executed in the context of the original shell process, otherwise it will not change the directory. However, then you have to seriously pervert with the pipe ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question