bash - stdout and stderr redirection in child process -
if run bash script this:
./script.sh 2>&1
stderr
redirected stdout
.
if script calls tool inside (e.g. ls
) or spawns new process, these child processes have stderr
redirected stdout
?
creating child process on unix/linux uses procedure generically known fork. copy entire process address space of current process (program code, data, everything) child. process identifier (pid) different in child, else same.
one of items in process copied file descriptor table. array. there entry each file open, , convention first three, 0, 1, 2, standard streams, stdin, stdout, stderr. explains numbers used in 2>&1
. when redirection, these 3 entries changed in child. done shell, since @ stage our child process shell process.
now comes magic part, generically known exec. if want run different program, ls
, can switch programs within process. new program starts beginning, core items retained. things user, group, current directory, umask, , file descriptor table retained use new program.
so, if file descriptor table altered previously, new program inherits changes. there nothing stop program overriding settings , using different files, done.
all behaviour default. programs can change file descriptors , other items retained across fork/exec boundary, don't.
Comments
Post a Comment