Clive
Clive

Reputation: 435

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

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?

Upvotes: 3

Views: 813

Answers (1)

Mikel Rychliski
Mikel Rychliski

Reputation: 3607

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).

Upvotes: 3

Related Questions