cd

Switch the user's current working directory.

Summary

cd [-L|[-P [-e]]] [dir]

The main purpose

Parameters

dir (optional): Specifies the directory to switch to.

Options

-L (default) If the target directory to switch to is a symbolic link, switch to the directory of the symbolic link.
-P If the target directory to be switched to is a symbolic link, switch to the physical location directory it points to.
- The current working directory will be switched to the directory represented by the environment variable OLDPWD, which is the previous working directory.

return value

The return status is successful unless the specified directory cannot be entered.

example

cd # Enter the user's home directory;
cd / # Enter the root directory
cd ~ # Enter the user's home directory;
cd .. # Return to the upper-level directory (if the current directory is "/", it will still be at "/" after execution; ".." means the upper-level directory);
cd ../.. # Return to the directory two levels above;
cd !$ # Use the parameters of the previous command as cd parameters.

Instructions for switching to the previous working directory

cd-
# The command will first display the target directory to be switched to, and then enter.
cd ${OLDPWD}
# The command will directly switch to the previous working directory.

About CDPATH

# Set the desktop folder as the value of CDPATH.
CDPATH='~/Desktop'
# Assume that there are no test3 folders under the paths ~ and ~/Desktop we are going to demonstrate next, create them now.
mkdir ~/test3
mkdir ~/Desktop/test3
# Enter the ~ directory.
cd ~
# Enter the test3 directory.
cd test3
# After execution, ~/Desktop/test3 is displayed and enters this directory instead of the test3 directory of the ~ directory.
# If CDPATH has a value, it will first search in CDPATH and enter the first successful match. If all fail, the current directory will be tried last.

About cdable_vars

# Open options.
shopt -s cdable_vars
# Assume that the current path and CDPATH do not have a directory named new_var.
new_var='~/Desktop'
# Try to enter.
cd new_var
# Close option.
shopt -u cdable_vars

Notice