1CD(P) POSIX Programmer's Manual CD(P)
2
3
4
6 cd - change the working directory
7
9 cd [-L | -P] [directory]
10
11 cd -
12
13
15 The cd utility shall change the working directory of the current shell
16 execution environment (see Shell Execution Environment ) by executing
17 the following steps in sequence. (In the following steps, the symbol
18 curpath represents an intermediate value used to simplify the descrip‐
19 tion of the algorithm used by cd. There is no requirement that curpath
20 be made visible to the application.)
21
22 1. If no directory operand is given and the HOME environment variable
23 is empty or undefined, the default behavior is implementation-
24 defined and no further steps shall be taken.
25
26 2. If no directory operand is given and the HOME environment variable
27 is set to a non-empty value, the cd utility shall behave as if the
28 directory named in the HOME environment variable was specified as
29 the directory operand.
30
31 3. If the directory operand begins with a slash character, set curpath
32 to the operand and proceed to step 7.
33
34 4. If the first component of the directory operand is dot or dot-dot,
35 proceed to step 6.
36
37 5. Starting with the first pathname in the colon-separated pathnames
38 of CDPATH (see the ENVIRONMENT VARIABLES section) if the pathname
39 is non-null, test if the concatenation of that pathname, a slash
40 character, and the directory operand names a directory. If the
41 pathname is null, test if the concatenation of dot, a slash charac‐
42 ter, and the operand names a directory. In either case, if the
43 resulting string names an existing directory, set curpath to that
44 string and proceed to step 7. Otherwise, repeat this step with the
45 next pathname in CDPATH until all pathnames have been tested.
46
47 6. Set curpath to the string formed by the concatenation of the value
48 of PWD , a slash character, and the operand.
49
50 7. If the -P option is in effect, the cd utility shall perform actions
51 equivalent to the chdir() function, called with curpath as the path
52 argument. If these actions succeed, the PWD environment variable
53 shall be set to an absolute pathname for the current working direc‐
54 tory and shall not contain filename components that, in the context
55 of pathname resolution, refer to a file of type symbolic link. If
56 there is insufficient permission on the new directory, or on any
57 parent of that directory, to determine the current working direc‐
58 tory, the value of the PWD environment variable is unspecified. If
59 the actions equivalent to chdir() fail for any reason, the cd util‐
60 ity shall display an appropriate error message and not alter the
61 PWD environment variable. Whether the actions equivalent to chdir()
62 succeed or fail, no further steps shall be taken.
63
64 8. The curpath value shall then be converted to canonical form as fol‐
65 lows, considering each component from beginning to end, in
66 sequence:
67
68 a. Dot components and any slashes that separate them from the next
69 component shall be deleted.
70
71 b. For each dot-dot component, if there is a preceding component
72 and it is neither root nor dot-dot, the preceding component,
73 all slashes separating the preceding component from dot-dot,
74 dot-dot and all slashes separating dot-dot from the following
75 component shall be deleted.
76
77 c. An implementation may further simplify curpath by removing any
78 trailing slash characters that are not also leading slashes,
79 replacing multiple non-leading consecutive slashes with a sin‐
80 gle slash, and replacing three or more leading slashes with a
81 single slash. If, as a result of this canonicalization, the
82 curpath variable is null, no further steps shall be taken.
83
84 9. The cd utility shall then perform actions equivalent to the chdir()
85 function called with curpath as the path argument. If these actions
86 failed for any reason, the cd utility shall display an appropriate
87 error message and no further steps shall be taken. The PWD envi‐
88 ronment variable shall be set to curpath.
89
90 If, during the execution of the above steps, the PWD environment vari‐
91 able is changed, the OLDPWD environment variable shall also be changed
92 to the value of the old working directory (that is the current working
93 directory immediately prior to the call to cd).
94
96 The cd utility shall conform to the Base Definitions volume of
97 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
98
99 The following options shall be supported by the implementation:
100
101 -L Handle the operand dot-dot logically; symbolic link components
102 shall not be resolved before dot-dot components are processed
103 (see steps 8. and 9. in the DESCRIPTION).
104
105 -P Handle the operand dot-dot physically; symbolic link components
106 shall be resolved before dot-dot components are processed (see
107 step 7. in the DESCRIPTION).
108
109
110 If both -L and -P options are specified, the last of these options
111 shall be used and all others ignored. If neither -L nor -P is speci‐
112 fied, the operand shall be handled dot-dot logically; see the DESCRIP‐
113 TION.
114
116 The following operands shall be supported:
117
118 directory
119 An absolute or relative pathname of the directory that shall
120 become the new working directory. The interpretation of a rela‐
121 tive pathname by cd depends on the -L option and the CDPATH and
122 PWD environment variables. If directory is an empty string, the
123 results are unspecified.
124
125 - When a hyphen is used as the operand, this shall be equivalent
126 to the command:
127
128
129 cd "$OLDPWD" && pwd
130
131 which changes to the previous working directory and then writes its
132 name.
133
134
136 Not used.
137
139 None.
140
142 The following environment variables shall affect the execution of cd:
143
144 CDPATH A colon-separated list of pathnames that refer to directories.
145 The cd utility shall use this list in its attempt to change the
146 directory, as described in the DESCRIPTION. An empty string in
147 place of a directory pathname represents the current directory.
148 If CDPATH is not set, it shall be treated as if it were an empty
149 string.
150
151 HOME The name of the directory, used when no directory operand is
152 specified.
153
154 LANG Provide a default value for the internationalization variables
155 that are unset or null. (See the Base Definitions volume of
156 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
157 ables for the precedence of internationalization variables used
158 to determine the values of locale categories.)
159
160 LC_ALL If set to a non-empty string value, override the values of all
161 the other internationalization variables.
162
163 LC_CTYPE
164 Determine the locale for the interpretation of sequences of
165 bytes of text data as characters (for example, single-byte as
166 opposed to multi-byte characters in arguments).
167
168 LC_MESSAGES
169 Determine the locale that should be used to affect the format
170 and contents of diagnostic messages written to standard error.
171
172 NLSPATH
173 Determine the location of message catalogs for the processing of
174 LC_MESSAGES .
175
176 OLDPWD A pathname of the previous working directory, used by cd -.
177
178 PWD This variable shall be set as specified in the DESCRIPTION. If
179 an application sets or unsets the value of PWD , the behavior of
180 cd is unspecified.
181
182
184 Default.
185
187 If a non-empty directory name from CDPATH is used, or if cd - is used,
188 an absolute pathname of the new working directory shall be written to
189 the standard output as follows:
190
191
192 "%s\n", <new directory>
193
194 Otherwise, there shall be no output.
195
197 The standard error shall be used only for diagnostic messages.
198
200 None.
201
203 None.
204
206 The following exit values shall be returned:
207
208 0 The directory was successfully changed.
209
210 >0 An error occurred.
211
212
214 The working directory shall remain unchanged.
215
216 The following sections are informative.
217
219 Since cd affects the current shell execution environment, it is always
220 provided as a shell regular built-in. If it is called in a subshell or
221 separate utility execution environment, such as one of the following:
222
223
224 (cd /tmp)
225 nohup cd
226 find . -exec cd {} \;
227
228 it does not affect the working directory of the caller's environment.
229
230 The user must have execute (search) permission in directory in order to
231 change to it.
232
234 None.
235
237 The use of the CDPATH was introduced in the System V shell. Its use is
238 analogous to the use of the PATH variable in the shell. The BSD C shell
239 used a shell parameter cdpath for this purpose.
240
241 A common extension when HOME is undefined is to get the login directory
242 from the user database for the invoking user. This does not occur on
243 System V implementations.
244
245 Some historical shells, such as the KornShell, took special actions
246 when the directory name contained a dot-dot component, selecting the
247 logical parent of the directory, rather than the actual parent direc‐
248 tory; that is, it moved up one level toward the '/' in the pathname,
249 remembering what the user typed, rather than performing the equivalent
250 of:
251
252
253 chdir("..");
254
255 In such a shell, the following commands would not necessarily produce
256 equivalent output for all directories:
257
258
259 cd .. && ls ls ..
260
261 This behavior is now the default. It is not consistent with the defini‐
262 tion of dot-dot in most historical practice; that is, while this behav‐
263 ior has been optionally available in the KornShell, other shells have
264 historically not supported this functionality. The logical pathname is
265 stored in the PWD environment variable when the cd utility completes
266 and this value is used to construct the next directory name if cd is
267 invoked with the -L option.
268
270 None.
271
273 Shell Execution Environment , pwd , the System Interfaces volume of
274 IEEE Std 1003.1-2001, chdir()
275
277 Portions of this text are reprinted and reproduced in electronic form
278 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
279 -- Portable Operating System Interface (POSIX), The Open Group Base
280 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
281 Electrical and Electronics Engineers, Inc and The Open Group. In the
282 event of any discrepancy between this version and the original IEEE and
283 The Open Group Standard, the original IEEE and The Open Group Standard
284 is the referee document. The original Standard can be obtained online
285 at http://www.opengroup.org/unix/online.html .
286
287
288
289IEEE/The Open Group 2003 CD(P)