CD(1P)
CD(1P) โ POSIX Programmer's Manual ๐
๐ซถ PROLOG ๐ซถ
okay so real talk: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so pls consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. ๐
๐ธ NAME ๐ธ
cd โ change the working directory. that's the whole vibe bestie.
โจ SYNOPSIS โจ
cd [-L|-P] [directory]
cd -
๐ฆ DESCRIPTION ๐ฆ
okay so the cd utility shall change the working directory of the current shell execution environment (see Section 2.12, Shell Execution Environment) by executing the following steps in sequence. (In the following steps, the symbol curpath represents an intermediate value used to simplify the description of the algorithm used by cd. There is no requirement that curpath be made visible to the application.) ๐ซถ
- If no
directoryoperand is given and theHOMEenvironment variable is empty or undefined, the default behavior is implementation-defined and no further steps shall be taken. like, we're done here bestie. ๐ - If no
directoryoperand is given and theHOMEenvironment variable is set to a non-empty value, thecdutility shall behave as if the directory named in theHOMEenvironment variable was specified as thedirectoryoperand. iconic. - If the
directoryoperand begins with a<slash>character, setcurpathto the operand and proceed to step 7. easy. - If the first component of the
directoryoperand is dot or dot-dot, proceed to step 6. noted. - Starting with the first pathname in the
<colon>-separated pathnames ofCDPATH(see the ENVIRONMENT VARIABLES section) if the pathname is non-null, test if the concatenation of that pathname, a<slash>character if that pathname did not end with a<slash>character, and thedirectoryoperand names a directory. If the pathname is null, test if the concatenation of dot, a<slash>character, and the operand names a directory. In either case, if the resulting string names an existing directory, setcurpathto that string and proceed to step 7. Otherwise, repeat this step with the next pathname inCDPATHuntil all pathnames have been tested. no cap. - Set
curpathto thedirectoryoperand. - If the
-Poption is in effect, proceed to step 10. Ifcurpathdoes not begin with a<slash>character, setcurpathto the string formed by the concatenation of the value ofPWD, a<slash>character if the value ofPWDdid not end with a<slash>character, andcurpath. - The
curpathvalue shall then be converted to canonical form as follows, considering each component from beginning to end, in sequence:
a. Dot components and any <slash> characters that separate them from the next component shall be deleted.
b. For each dot-dot component, if there is a preceding component and it is neither root nor dot-dot, then:
i. If the preceding component does not refer (in the context of pathname resolution with symbolic links followed) to a directory, then the cd utility shall display an appropriate error message and no further steps shall be taken.
ii. 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.
c. An implementation may further simplify curpath by removing any trailing <slash> characters that are not also leading <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, the curpath variable is null, no further steps shall be taken.
- If
curpathis longer than{PATH_MAX}bytes (including the terminating null) and thedirectoryoperand was not longer than{PATH_MAX}bytes (including the terminating null), thencurpathshall be converted from an absolute pathname to an equivalent relative pathname if possible. This conversion shall always be considered possible if the value ofPWD, with a trailing<slash>added if it does not already have one, is an initial substring ofcurpath. Whether or not it is considered possible under other circumstances is unspecified. Implementations may also apply this conversion ifcurpathis not longer than{PATH_MAX}bytes or thedirectoryoperand was longer than{PATH_MAX}bytes. - The
cdutility shall then perform actions equivalent to thechdir()function called withcurpathas the path argument. If these actions fail for any reason, thecdutility shall display an appropriate error message and the remainder of this step shall not be executed. If the-Poption is not in effect, thePWDenvironment variable shall be set to the value thatcurpathhad on entry to step 9 (i.e., before conversion to a relative pathname). If the-Poption is in effect, thePWDenvironment variable shall be set to the string that would be output bypwd -P. If there is insufficient permission on the new directory, or on any parent of that directory, to determine the current working directory, the value of thePWDenvironment variable is unspecified.
If, during the execution of the above steps, the PWD environment variable is set, the OLDPWD environment variable shall also be set to the value of the old working directory (that is the current working directory immediately prior to the call to cd).
๐ OPTIONS ๐
The cd utility shall conform to the Base Definitions volume of POSIX.1โ2017, Section 12.2, Utility 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 (see step 7. in the DESCRIPTION).
If both -L and -P options are specified, the last of these options shall be used and all others ignored. If neither -L nor -P is specified, the operand shall be handled dot-dot logically; see the DESCRIPTION.
๐ OPERANDS ๐
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 relative pathname bycddepends on the-Loption and theCDPATHandPWDenvironment variables. Ifdirectoryis 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" && pwd
which changes to the previous working directory and then writes its name.
๐ STDIN ๐
Not used.
๐ INPUT FILES ๐
None.
๐ ENVIRONMENT VARIABLES ๐
The following environment variables shall affect the execution of cd:
CDPATHโ A<colon>-separated list of pathnames that refer to directories. Thecdutility shall use this list in its attempt to change the directory, as described in the DESCRIPTION. An empty string in place of a directory pathname represents the current directory. IfCDPATHis not set, it shall be treated as if it were an empty string.HOMEโ The name of the directory, used when nodirectoryoperand is specified.LANGโ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables 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 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 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, the behavior ofcdis unspecified.
๐ง ASYNCHRONOUS EVENTS ๐ง
Default.
โญ STDOUT โญ
If a non-empty directory name from CDPATH is used, or if cd - is used, an absolute pathname of the new working directory shall be written to the standard output as follows:
"%s\n", <new directory>
Otherwise, there shall be no output.
๐ STDERR ๐
The standard error shall be used only for diagnostic messages.
๐ฆ OUTPUT FILES ๐ฆ
None.
๐ธ EXTENDED DESCRIPTION ๐ธ
None.
๐ EXIT STATUS ๐
The following exit values shall be returned:
0โ The directory was successfully changed.>0โ An error occurred.
๐ฅ CONSEQUENCES OF ERRORS ๐ฅ
The working directory shall remain unchanged.
The following sections are informative.
๐ APPLICATION USAGE ๐
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:
(cd /tmp)
nohup cd
find . -exec cd {} \;
it does not affect the working directory of the caller's environment.
The user must have execute (search) permission in directory in order to change to it.
๐ EXAMPLES ๐
The following template can be used to perform processing in the directory specified by location and end up in the current working directory in use before the first cd command was issued:
cd location
if [ $? -ne 0 ]
then
print error message
exit 1
fi
... do whatever is desired as long as the OLDPWD environment variable
is not modified
cd -
๐ญ RATIONALE (aka the lore)
ok so bestie, let me tell you where CDPATH even came from. It got introduced in the System V shell, and using it is basically the same energy as using the PATH variable in the shell โ like, same vibe, different variable. The BSD C shell did the same thing but with a shell parameter called cdpath instead. iykyk ๐
Now here's a thing that's not universal: a lot of shells have this extra little extension where, if HOME is undefined, they go grab the login directory from the user database for whoever's invoking them. But that does NOT happen on System V implementations, okay? Just so we're clear. ๐
Some historical shells โ KornShell, looking at you โ did something โจspecialโจ when the directory name had a dot-dot component in it. Instead of going to the actual parent directory, they'd pick the logical parent โ like, they moved up one level toward the '/' in the pathname, remembering what the user typed, instead of doing the equivalent of:
chdir("..");
So in a shell like that, these two commands would not necessarily produce equivalent output for all directories:
cd .. && ls ls ..
And now? This behavior is the default. Periodt. But heads up: it's not consistent with how dot-dot is defined in most historical practice. Like, yeah, it was optionally available in KornShell, but other shells historically just did not support this functionality. The whole point is that the logical pathname gets stored in the PWD environment variable when the cd utility finishes up, and that value is what gets used to construct the next directory name if cd is invoked with the -L option. ๐
๐ฎ FUTURE DIRECTIONS
None.
(Yeah. That's it. Nothing coming. The future is empty, bestie, we're living in the present. ๐)
๐ SEE ALSO
- Section 2.12, Shell Execution Environment,
pwd - The Base Definitions volume of POSIX.1โ2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
- The System Interfaces volume of POSIX.1โ2017,
chdir()
๐ COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay that's a wrap on the whole page ๐ this one came from IEEE/The Open Group, 2017, and it's the CD(1P) page โ go forth and change directories responsibly, bestie ๐ฆ๐
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms