Equivalent of shell 'cd' command to change the working directory?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The shell command cd changes the current working directory of the shell process itself. In programming terms, the underlying operation is usually chdir, but the exact answer depends on whether you mean shell usage, a child process, or changing the working directory inside your own program.
Why cd Is a Shell Built-In
cd is not normally an external executable. It is built into shells such as Bash, Zsh, and PowerShell because a separate process cannot change the parent shell's working directory.
That detail explains a common surprise: running a script or subprocess that calls cd does not permanently move your current terminal session. The subprocess changes its own directory, then exits, and the parent shell stays where it was.
The Programmatic Equivalent Is chdir
At the operating-system level, the equivalent concept is the current process calling a directory-change function. In C on Unix-like systems, that function is chdir.
That changes the working directory of the running process. Any relative file paths used afterward are resolved from the new location.
Equivalent Calls in Common Languages
Many languages expose the same operation through a standard library wrapper.
Python:
Node.js:
Ruby:
These calls affect only the current process. They do not change the directory of the terminal or IDE process that launched them.
Shell Usage Versus Subprocess Usage
Inside a shell script, cd works because the shell is executing the script logic in a shell context.
But if you launch a child shell from another program and run cd inside that child shell, the parent process still does not move. This is the key distinction:
- '
cdchanges the current shell session' - '
chdirchanges the current process' - a child process cannot change its parent's directory
That last rule is why tools that want to affect your interactive shell usually ask to be sourced rather than executed as normal commands.
When You Should Not Change the Working Directory
Changing the process working directory can simplify relative file access, but it also changes global process state. In larger applications, that can make path handling harder to reason about, especially in multistep scripts, tests, or services.
A safer alternative is often to keep the working directory stable and use absolute paths or path-join helpers. That makes file access more explicit and reduces surprises when code is reused in a different environment.
For short scripts and command-line tools, however, changing directory is often perfectly reasonable.
Common Pitfalls
- Expecting a child process to change the parent shell directory will never work. Process boundaries prevent that by design.
- Using
cdin a script without checking for failure can send later commands into the wrong directory. Guard the change with|| exit 1or equivalent error handling. - Changing the global working directory deep inside an application can create confusing path bugs. Prefer explicit paths when many components share one process.
- Assuming
cdis an external command misses why it behaves differently from normal executables. It is a shell built-in because it must affect shell state directly. - Forgetting that relative paths depend on the current working directory can make file operations environment-sensitive. Log or inspect the current directory when debugging path issues.
Summary
- The shell command
cdchanges the working directory of the current shell. - The programmatic equivalent is typically
chdiror a language wrapper such asos.chdirorprocess.chdir. - A subprocess can change only its own working directory, not the parent's.
- '
cdis a shell built-in for that reason.' - Use directory changes deliberately, because they alter global process state and affect all later relative paths.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.