0

When running a program from the terminal, say i type make how does make know the directory which was in the terminal?

0

1 Answer 1

0

The "current directory" isn't a property of the terminal in the first place; it's tracked by the kernel per-process and every process (even those not associated with a terminal) has its own "working directory", which is automatically inherited down by child processes.

Any process can use the chdir() system call (POSIX) or SetCurrentDirectory() (Windows) to change its own working directory. Use getcwd() (POSIX) or GetCurrentDirectory() (Windows) to retrieve the current directory.

(The terminal app does have its own "working directory", but this only influences where the shell will start and doesn't get updated when you cd around. Nearly all graphical apps stay at $HOME the whole time.)


If you want to find out the working directory of some other process, Linux lets you look at /proc/<pid>/cwd, but this is generally not something that your program should be doing.

3
  • In a simple c++ program, how can i get the path which was in the terminal which ran my program
    – ihsan
    May 26, 2022 at 12:55
  • I don't know the C++ conventions, but in plain C you would use the getcwd() function in POSIX or GetCurrentDirectory() in Win32 API. May 26, 2022 at 12:57
  • Looks like the modern C++ convention is std::filesystem::current_path(), or at least that's what google search results are saying. May 26, 2022 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .