Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I set the present working directory by setting PWD

Tags:

c

linux

process

I want to set the current working directory for a child program. I am setting it with setenv("PWD", "/media", 1); in the parent and starting the child with system(...);. But when the child calls getcwd(0, 0) it is returning /root.

I thought child processes were supposed to inherit the environment from their parent?

like image 951
Clive Avatar asked Jan 20 '26 04:01

Clive


1 Answers

POSIX compliant shells set the PWD environment variable to the current working directory. However, the actual current working directory is a property of the process itself (inherited by children spawned using system or fork) and needs to be changed with chdir.

Changes to the PWD environment variable variable don't change anything, and the variable does not necessarily actually reflect the current working directory (if it was changed by something other than the shell).

like image 151
Mikel Rychliski Avatar answered Jan 21 '26 21:01

Mikel Rychliski