Shell Commands
CD Command
Programming
Directory Navigation
Command Line Interface

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.

Browse interview questions

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.

c
1#include <stdio.h>
2#include <unistd.h>
3
4int main(void) {
5    if (chdir("/tmp") != 0) {
6        perror("chdir failed");
7        return 1;
8    }
9
10    printf("Working directory changed.\n");
11    return 0;
12}

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:

python
1import os
2
3print(os.getcwd())
4os.chdir("/tmp")
5print(os.getcwd())

Node.js:

javascript
console.log(process.cwd());
process.chdir("/tmp");
console.log(process.cwd());

Ruby:

ruby
puts Dir.pwd
Dir.chdir("/tmp")
puts Dir.pwd

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.

bash
1#!/usr/bin/env bash
2
3cd /tmp || exit 1
4pwd

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:

  • 'cd changes the current shell session'
  • 'chdir changes 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 cd in a script without checking for failure can send later commands into the wrong directory. Guard the change with || exit 1 or 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 cd is 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 cd changes the working directory of the current shell.
  • The programmatic equivalent is typically chdir or a language wrapper such as os.chdir or process.chdir.
  • A subprocess can change only its own working directory, not the parent's.
  • 'cd is a shell built-in for that reason.'
  • Use directory changes deliberately, because they alter global process state and affect all later relative paths.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions