cd - change the working directory
cd [directory] cd -
The cd utility will change the working directory of the current shell execution environment; seeShell Execution Environment . If the current working directory is successfully changed, it will save an absolute pathname of the old working directory in the environment variable OLDPWD and it will save an absolute pathname of the new working directory in the environment variable PWD .When invoked with no operands, and the HOME environment variable is set to a non-empty value, the directory named in the HOME environment variable will become the new working directory. If HOME is empty or is undefined, the default behaviour is implementation-dependent.
None.
The following operands are supported:
- directory
- An absolute or relative pathname of the directory that becomes the new working directory. The interpretation of a relative pathname by cd depends on the CDPATH environment variable.
- -
- When a hyphen is used as the operand, this is equivalent to the command:
which changes to the previous working directory and then writes its name.cd "$OLDPWD" && pwd
Not used.
None.
The following environment variables affect the execution of cd:
- CDPATH
- A colon-separated list of pathnames that refer to directories. If the directory operand does not begin with a slash (/) character, and the first component is not dot or dot-dot, cd will search for directory relative to each directory named in the CDPATH variable, in the order listed. The new working directory will be set to the first matching directory found. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it will be treated as if it were an empty string.
- HOME
- The name of the home directory, used when no directory operand is specified.
- LANG
- Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
- LC_ALL
- If set to a non-empty string value, override the values of all the other internationalisation variables.
- LC_CTYPE
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).
- LC_MESSAGES
- Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
- NLSPATH
- Determine the location of message catalogues for the processing of LC_MESSAGES .
- OLDPWD
- A pathname of the previous working directory, used by cd -.
- PWD
- A pathname of the current working directory, set by cd after it has changed to that directory.
Default.
If a non-empty directory name from CDPATH is used, or if cd - is used, an absolute pathname of the new working directory will be written to the standard output as follows:
"%s\n", <new directory>
Otherwise, there will be no output.
Used only for diagnostic messages.
None.
None.
The following exit values are returned:
- 0
- The directory was successfully changed.
- >0
- An error occurred.
The working directory remains unchanged.
Since cd affects the current shell execution environment, it is always provided as a shell regular built-in. If it is called in a subshell or separate utility execution environment, such as one of the following:it will not affect the working directory of the caller's environment.(cd /tmp) nohup cd find . -exec cd {} \;
The user must have execute (search) permission in directory in order to change to it.
None.
None.
pwd, the XSH specification description of chdir().