Why can't I change directories using cd in a script?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with shell scripts, a common question that arises is why changes in directory using the cd command don't seem to persist once the script finishes. This can be perplexing especially for those who are new to shell scripting. The reason for this behavior involves understanding how shell processes handle execution environments and child processes.
Understanding Process Environments and Child Processes
In Unix-like operating systems, when a shell script runs, it is executed in a new shell instance — a child process of the shell from which it was launched. Each process in Unix has its own set of environment settings, including the current working directory.
Example Scenario
Imagine you have a script named change-dir.sh with the following content:
Executing this script will output the path /some/directory, showing that within the script, the cd command worked as expected. However, once the script finishes and control returns to the parent shell (the original terminal), the working directory will be unchanged.
Why the Directory Change Doesn't Persist
The key point here is that when you use cd in a script, the change in directory affects only the child process in which the script runs. The parent shell, which is your working terminal, retains its original settings (including the current directory) unaffected by the changes made in the child process.
How to Reflect Changes in the Parent Shell
To have a directory change in a script reflected in the parent shell after the script completes, you can:
- Source the script instead of running it: By using the
sourcecommand (or its shortcut.), you execute the script in the current shell environment, not a child shell.
This command will change the current working directory in the active shell to /some/directory.
- Use subshells for temporary changes: You can use parentheses to create a subshell in which the directory change will be temporary and not affect the parent shell's current directory:
The above change affects only the subshell environment enclosed in parentheses.
Practical Applications and Considerations
Scripts that need to perform actions in specific directories often use cd to ensure commands operate in the correct directory. However, script authors must consider the scope of such commands and manage directory changes carefully to avoid unexpected behavior.
Summary Table
| Aspect | Detail |
| Script Execution Context | Runs in a child process, separate from the parent shell. |
Effect of cd in Script | Changes directory only in the child process, not in the parent shell. |
| Making Changes Persist | Use source to execute the script in the current shell environment. |
| Temporary Change with Subshell | Use (cd /path; command) to change directory temporarily within subshell. |
Conclusion
The use of cd in scripts is common and useful but understanding its implications in different process environments is crucial for effective script writing. As scripts execute as child processes, their environment changes do not affect the parent shell. Strategies such as sourcing the script or using subshells are essential for managing where and how directory changes apply, ensuring scripts function as intended in broader system operations.
.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.