I've been stuck on this for hours:
cd /dir1
(cd $HOME); pwd;
Why does pwd
still say /dir1
and didn't go to my home directory?
I've been stuck on this for hours:
cd /dir1
(cd $HOME); pwd;
Why does pwd
still say /dir1
and didn't go to my home directory?
Parentheses start a subshell: the shell calls fork
, and the commands inside the parentheses are executed in the subprocess. The parent process waits for the subprocess to exit then resumes execution. So what's happening is:
cd /dir1
: The shell performs chdir("/dir1")
.fork
, then the parent process waits for the child to exit.cd $HOME
: the subshell performs chdir("/home/jurgen")
.wait
call in the parent returns.pwd
: the shell prints its current directory, which is /dir1
.