bash -c <command>
直接在 bash shell 中运行命令与在 bash shell 中运行命令有什么区别?我的问题的目的是我想知道bash -c
在 shell 中和不在 shell 中如何执行命令。这些信息可以帮助我思考之前的问题https://unix.stackexchange.com/questions/261595/do-a-bash-script-and-bash-c-command-run-in-a-subshell-environment, 和为什么通过命令替换调用传递到子 shell 的非环境变量?
以下是我尝试通过的一种方式来理解上述问题strace
。但您可以在不使用 的情况下解释差异strace
。
我考虑两种情况,runningbash -c date
和 running bash -c 'date&'
。
当我
bash -c date
在 pid 6913 的 bash shell 中运行时,并在另一个 shell 中跟踪该 shell:$ sudo strace -f -e trace=process -p 6913 Process 6913 attached clone(Process 13010 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,child_tidptr=0x7f457c05ca10) = 13010 [pid 6913] wait4(-1, <unfinished ...> [pid 13010] execve("/bin/bash", ["bash", "-c", "date"], [/* 66 vars */]) = 0 [pid 13010] arch_prctl(ARCH_SET_FS, 0x7f920c591740) = 0 [pid 13010] execve("/bin/date", ["date"], [/* 66 vars */]) = 0 [pid 13010] arch_prctl(ARCH_SET_FS, 0x7f67e34df740) = 0 [pid 13010] exit_group(0) = ? [pid 13010] +++ exited with 0 +++ <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 13010 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=13010, si_status=0, si_utime=0, si_stime=0} --- wait4(-1, 0x7ffea6781518, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
与在 bash shell 中运行(见下文)相比
date
,区别仅在于我们在第一行clone()
和之间多了一行execve()
date
:execve("/bin/bash", ["bash", "-c", "date"], [/* 66 vars */]) = 0
区别是什么意思?
跟踪运行的输出
date
而不是bash -c date
:$ sudo strace -f -e trace=process -p 6913 [sudo] password for t: Process 6913 attached clone(Process 12918 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,child_tidptr=0x7f457c05ca10) = 12918 [pid 6913] wait4(-1, <unfinished ...> [pid 12918] execve("/bin/date", ["date"], [/* 66 vars */]) = 0 [pid 12918] arch_prctl(ARCH_SET_FS, 0x7ff00c632740) = 0 [pid 12918] exit_group(0) = ? [pid 12918] +++ exited with 0 +++ <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 12918 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12918, si_status=0, si_utime=0, si_stime=0} --- wait4(-1, 0x7ffea6781518, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
当我
bash -c 'date&'
在 pid 6913 的 bash shell 中运行时,并在另一个 shell 中跟踪该 shell:$ sudo strace -f -e trace=process -p 6913 Process 6913 attached clone(Process 13023 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,child_tidptr=0x7f457c05ca10) = 13023 [pid 6913] wait4(-1, <unfinished ...> [pid 13023] execve("/bin/bash", ["bash", "-c", "date&"], [/* 66 vars */]) = 0 [pid 13023] arch_prctl(ARCH_SET_FS, 0x7f154970b740) = 0 [pid 13023] clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,child_tidptr=0x7f154970ba10) = 13024 [pid 13023] exit_group(0) = ? [pid 13023] +++ exited with 0 +++ <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 13023 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=13023, si_status=0, si_utime=0, si_stime=0} --- wait4(-1, 0x7ffea6781518, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes) Process 13024 attached [pid 13024] execve("/bin/date", ["date"], [/* 66 vars */]) = 0 [pid 13024] arch_prctl(ARCH_SET_FS, 0x7f8045f35740) = 0 [pid 13024] exit_group(0) = ? [pid 13024] +++ exited with 0 +++
与在 bash shell 中运行(见下文)相比
date&
,区别似乎不仅仅是多了一个execve
:execve("/bin/bash", ["bash", "-c", "date&"], [/* 66 vars */]) = 0
还安排子流程的创建,哪个子流程
execve()
date
和哪个子流程execve()
/bin/bash
。区别是什么意思?
运行时跟踪输出
date&
而不是bash -c 'date&'
:$ sudo strace -f -e trace=process -p 6913 Process 6913 attached clone(Process 12931 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,child_tidptr=0x7f457c05ca10) = 12931 [pid 12931] execve("/bin/date", ["date"], [/* 66 vars */]) = 0 [pid 12931] arch_prctl(ARCH_SET_FS, 0x7f530c5ee740) = 0 [pid 12931] exit_group(0) = ? [pid 12931] +++ exited with 0 +++ --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12931, si_status=0, si_utime=0, si_stime=0} --- wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 12931 wait4(-1, 0x7ffea6780718, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)