Syntax
Description
Thecd command changes the current working directory within Nash’s virtual filesystem. When you change directories, Nash automatically updates the $PWD environment variable to reflect the new location and saves the previous directory in $OLDPWD.
All directory changes happen within Nash’s Virtual Filesystem (VFS). Host directories are only accessible through explicit mount bindings using
--bind or --bind-ro flags when starting Nash.Arguments
The target directory to change to. Can be an absolute path (starting with
/) or relative path.Special values:- No argument: Changes to
$HOMEdirectory (default:/home/user) -: Changes to the previous directory stored in$OLDPWD
Behavior
- No arguments - Navigates to the user’s home directory from the
$HOMEenvironment variable - Relative paths - Resolved relative to the current working directory (
$PWD) - Absolute paths - Navigate directly to the specified path
cd -- Toggle to the previous working directory using$OLDPWD- Environment updates - Automatically sets
$PWDto new directory and$OLDPWDto previous directory
Examples
Navigate to absolute path
Navigate to relative path
Go back to previous directory
Return to home directory
Navigate using environment variables
Use with subshells
Changes made inside a subshell
( ) do not affect the parent shell’s working directory.Error Handling
Directory does not exist
Not a directory (file path provided)
No previous directory
If
$OLDPWD is not set, cd - defaults to the root directory /.VFS Integration
Working with mounted directories
When starting Nash with host directory mounts, you can navigate into them normally:Read-only mounts
You cancd into read-only mounted directories without any restrictions:
Environment Variables
Automatically updated to the new current working directory after each successful
cd command.Automatically updated to store the previous working directory before changing to a new one. Used by
cd -.Used as the target directory when
cd is called without arguments. Default: /home/<username>Related Commands
pwd- Print the current working directorymkdir- Create new directoriesls- List directory contentstree- Display directory structure as a tree
Implementation Details
Thecd command is implemented in /home/daytona/workspace/source/src/builtins/cd.rs:9-41. Key implementation notes:
- Validates that the target path exists and is a directory before changing
- Uses
VfsPath::join()to resolve relative paths against the current directory - Always updates both
$PWDand$OLDPWDenvironment variables - Falls back to
/home/userif$HOMEis not defined - Falls back to
/if$OLDPWDis not set when usingcd -
