cd - change the working directory
cd[-L|-P] [directory]
cd -
Thecd utility shall change the working directory of the current shell execution environment (seeShell Execution Environment) by executing the following steps in sequence. (Inthe following steps, the symbolcurpath represents an intermediate value used to simplify the description of the algorithmused bycd. There is no requirement thatcurpath be made visible to the application.)
If nodirectory operand is given and theHOME environment variable is empty or undefined, the default behavior isimplementation-defined and no further steps shall be taken.
If nodirectory operand is given and theHOME environment variable is set to a non-empty value, thecdutility shall behave as if the directory named in theHOME environment variable was specified as thedirectoryoperand.
If thedirectory operand begins with a <slash> character, setcurpath to the operand and proceed to step7.
If the first component of thedirectory operand is dot or dot-dot, proceed to step 6.
Starting with the first pathname in the <colon>-separated pathnames ofCDPATH (see the ENVIRONMENT VARIABLESsection) if the pathname is non-null, test if the concatenation of that pathname, a <slash> character if that pathname didnot end with a <slash> character, and thedirectory operand names a directory. If the pathname is null, test if theconcatenation of dot, a <slash> character, and the operand names a directory. In either case, if the resulting string namesan existing directory, setcurpath to that string and proceed to step 7. Otherwise, repeat this step with the next pathnameinCDPATH until all pathnames have been tested.
Setcurpath to thedirectory operand.
If the-P option is in effect, proceed to step 10. Ifcurpath does not begin with a <slash> character, setcurpath to the string formed by the concatenation of the value ofPWD, a <slash> character if the value ofPWD did not end with a <slash> character, andcurpath.
Thecurpath value shall then be converted to canonical form as follows, considering each component from beginning to end,in sequence:
Dot components and any <slash> characters that separate them from the next component shall be deleted.
For each dot-dot component, if there is a preceding component and it is neither root nor dot-dot, then:
If the preceding component does not refer (in the context of pathname resolution with symbolic links followed) to a directory,then thecd utility shall display an appropriate error message and no further steps shall be taken.
The preceding component, all <slash> characters separating the preceding component from dot-dot, dot-dot, and all<slash> characters separating dot-dot from the following component (if any) shall be deleted.
An implementation may further simplifycurpath by removing any trailing <slash> characters that are not alsoleading <slash> characters, replacing multiple non-leading consecutive <slash> characters with a single <slash>,and replacing three or more leading <slash> characters with a single <slash>. If, as a result of this canonicalization,thecurpath variable is null, no further steps shall be taken.
Ifcurpath is longer than {PATH_MAX} bytes (including the terminating null) and thedirectory operand was notlonger than {PATH_MAX} bytes (including the terminating null), thencurpath shall be converted from an absolute pathname toan equivalent relative pathname if possible. This conversion shall always be considered possible if the value ofPWD, witha trailing <slash> added if it does not already have one, is an initial substring ofcurpath. Whether or not it isconsidered possible under other circumstances is unspecified. Implementations may also apply this conversion ifcurpath isnot longer than {PATH_MAX} bytes or thedirectory operand was longer than {PATH_MAX} bytes.
Thecd utility shall then perform actions equivalent to thechdir() functioncalled withcurpath as thepath argument. If these actions fail for any reason, thecd utility shall displayan appropriate error message and the remainder of this step shall not be executed. If the-P option is not in effect, thePWD environment variable shall be set to the value thatcurpath had on entry to step 9 (i.e., before conversion to arelative pathname). If the-P option is in effect, thePWD environment variable shall be set to the string that wouldbe output bypwd-P. If there is insufficient permission on the new directory, oron any parent of that directory, to determine the current working directory, the value of thePWD environment variable isunspecified.
If, during the execution of the above steps, thePWD environment variable is set, theOLDPWD environment variableshall also be set to the value of the old working directory (that is the current working directory immediately prior to the call tocd).
Thecd utility shall conform to XBDUtility Syntax Guidelines.
The following options shall be supported by the implementation:
- -L
- Handle the operand dot-dot logically; symbolic link components shall not be resolved before dot-dot components are processed(see steps 8. and 9. in the DESCRIPTION).
- -P
- Handle the operand dot-dot physically; symbolic link components shall be resolved before dot-dot components are processed (seestep 7. in the DESCRIPTION).
If both-L and-P options are specified, the last of these options shall be used and all others ignored. Ifneither-L nor-P is specified, the operand shall be handled dot-dot logically; see the DESCRIPTION.
The following operands shall be supported:
- directory
- An absolute or relative pathname of the directory that shall become the new working directory. The interpretation of a relativepathname bycd depends on the-L option and theCDPATH andPWD environment variables. Ifdirectory is an empty string, the results are unspecified.
- -
- When a <hyphen-minus> is used as the operand, this shall be equivalent to the command:
cd "$OLDPWD" && pwdwhich changes to the previous working directory and then writes its name.
Not used.
None.
The following environment variables shall affect the execution ofcd:
- CDPATH
- A <colon>-separated list of pathnames that refer to directories. Thecd utility shall use this list in its attemptto change the directory, as described in the DESCRIPTION. An empty string in place of a directory pathname represents the currentdirectory. IfCDPATH is not set, it shall be treated as if it were an empty string.
- HOME
- The name of the directory, used when nodirectory operand is specified.
- LANG
- Provide a default value for the internationalization variables that are unset or null. (See XBDInternationalization Variables for the precedence of internationalizationvariables used to determine the values of locale categories.)
- LC_ALL
- If set to a non-empty string value, override the values of all the other internationalization variables.
- LC_CTYPE
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte asopposed 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
- [XSI]
Determine the location of message catalogs for the processing ofLC_MESSAGES.
- OLDPWD
- A pathname of the previous working directory, used bycd-.
- PWD
- This variable shall be set as specified in the DESCRIPTION. If an application sets or unsets the value ofPWD, thebehavior ofcd is unspecified.
Default.
If a non-empty directory name fromCDPATH is used, or ifcd- is used, an absolute pathname of the newworking directory shall be written to the standard output as follows:
"%s\n", <new directory>Otherwise, there shall be no output.
The standard error shall be used only for diagnostic messages.
None.
None.
The following exit values shall be returned:
- 0
- The directory was successfully changed.
- >0
- An error occurred.
The working directory shall remain unchanged.
Sincecd affects the current shell execution environment, it is always provided as a shell regular built-in. If it iscalled in a subshell or separate utility execution environment, such as one of the following:
(cd /tmp)nohup cdfind . -exec cd {} \;it does not affect the working directory of the caller's environment.
The user must have execute (search) permission indirectory in order to change to it.
The following template can be used to perform processing in the directory specified bylocation and end up in the currentworking directory in use before the firstcd command was issued:
cdlocationif [ $? -ne 0 ]then print error message exit 1fi... do whatever is desired as long as the OLDPWD environment variable is not modifiedcd -
The use of theCDPATH was introduced in the System V shell. Its use is analogous to the use of thePATH variablein the shell. The BSD C shell used a shell parametercdpath for this purpose.
A common extension whenHOME is undefined is to get the login directory from the user database for the invoking user.This does not occur on System V implementations.
Some historical shells, such as the KornShell, took special actions when the directory name contained a dot-dot component,selecting the logical parent of the directory, rather than the actual parent directory; that is, it moved up one level toward the'/' in the pathname, remembering what the user typed, rather than performing the equivalent of:
chdir("..");In such a shell, the following commands would not necessarily produce equivalent output for all directories:
cd .. && ls ls ..This behavior is now the default. It is not consistent with the definition of dot-dot in most historical practice; that is,while this behavior has been optionally available in the KornShell, other shells have historically not supported thisfunctionality. The logical pathname is stored in thePWD environment variable when thecd utility completes and thisvalue is used to construct the next directory name ifcd is invoked with the-L option.
None.
Shell Execution Environment,pwd
XBDEnvironment Variables,Utility Syntax Guidelines
XSHchdir
First released in Issue 2.
The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:
Thecd- operand,PWD, andOLDPWD are added.
The-L and-P options are added to align with the IEEE P1003.2b draft standard. This also includes theintroduction of a new description to include the effect of these options.
IEEE Std 1003.1-2001/Cor 1-2002, item XCU/TC1/D6/14 is applied, changing the SYNOPSIS to make it clear that the-L and-P options are mutually-exclusive.
Austin Group Interpretation 1003.1-2001 #037 is applied.
Austin Group Interpretation 1003.1-2001 #199 is applied, clarifying how thecd utility handles concatenation of twopathnames when the first pathname ends in a <slash> character.
SD5-XCU-ERN-97 is applied, updating the SYNOPSIS.
Step 7 of the processing performed bycd is revised to refer tocurpath instead of ``the operand''.
Changes to thepwd utility andPWD environment variable have been made tomatch the changes to thegetcwd() function made for Austin Group Interpretation1003.1-2001 #140.
POSIX.1-2008, Technical Corrigendum 1, XCU/TC1-2008/0076 [230], XCU/TC1-2008/0077 [240], XCU/TC1-2008/0078 [240], andXCU/TC1-2008/0079 [123] are applied.
POSIX.1-2008, Technical Corrigendum 2, XCU/TC2-2008/0074 [584] is applied.
return to top of page