linux - Why does bash execute 2 instances of my script when using a pipe inside of $() -
here bash script:
#!/bin/bash $(find / -name "foo" | grep "bar")
here ps has output:
$ ps fx pid tty stat time command 2690 ? sl 1:04 gnome-terminal 5903 pts/8 ss 0:00 \_ bash 7003 pts/8 s 0:00 \_ bash -x ./test_script.sh 7004 pts/8 s 0:00 | \_ bash -x ./test_script.sh 7005 pts/8 s 0:00 | \_ find / -name foo 7006 pts/8 s 0:00 | \_ grep bar $ ps aux user pid %cpu %mem vsz rss tty stat start time command 1000 7003 0.0 0.0 5172 1108 pts/8 s 16:23 0:00 bash -x ./test_script.sh 1000 7004 0.0 0.0 5172 520 pts/8 s 16:23 0:00 bash -x ./test_script.sh 1000 7005 0.7 0.0 4720 1176 pts/8 s 16:23 0:00 find / -name foo 1000 7006 0.0 0.0 4368 824 pts/8 s 16:23 0:00 grep bar
as can see there 2 instances of script being executed, can tell me bash doing here? why there 2 instances of script being executed , there better way of doing this?
thanks
when run subshell (the $(...)
part) bash uses fork()
system call creates copy of calling process (where subshell commands executed). script isn't being run again, rather command line inherited parent here since there no exec. in child shell bash sets pipeline, why see find
, grep
children.
Comments
Post a Comment