summaryrefslogtreecommitdiffstats
path: root/man-pages-posix-2017/man1p/cd.1p
blob: d73874f572f7c7094315d644f9207fb466174912 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
'\" et
.TH CD "1P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
cd
\(em change the working directory
.SH SYNOPSIS
.LP
.nf
cd \fB[\fR-L|-P\fB] [\fIdirectory\fB]\fR
.P
cd -
.fi
.SH DESCRIPTION
The
.IR cd
utility shall change the working directory of the current shell
execution environment (see
.IR "Section 2.12" ", " "Shell Execution Environment")
by executing the following steps in sequence. (In the following steps,
the symbol
.BR curpath
represents an intermediate value used to simplify the description of
the algorithm used by
.IR cd .
There is no requirement that
.BR curpath
be made visible to the application.)
.IP " 1." 4
If no
.IR directory
operand is given and the
.IR HOME
environment variable is empty or undefined, the default behavior is
implementation-defined and no further steps shall be taken.
.IP " 2." 4
If no
.IR directory
operand is given and the
.IR HOME
environment variable is set to a non-empty value, the
.IR cd
utility shall behave as if the directory named in the
.IR HOME
environment variable was specified as the
.IR directory
operand.
.IP " 3." 4
If the
.IR directory
operand begins with a
<slash>
character, set
.BR curpath
to the operand and proceed to step 7.
.IP " 4." 4
If the first component of the
.IR directory
operand is dot or dot-dot, proceed to step 6.
.IP " 5." 4
Starting with the first pathname in the
<colon>-separated
pathnames of
.IR CDPATH
(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 the
.IR directory
operand 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, set
.BR curpath
to that string and proceed to step 7. Otherwise, repeat this step with
the next pathname in
.IR CDPATH
until all pathnames have been tested.
.IP " 6." 4
Set
.BR curpath
to the
.IR directory
operand.
.IP " 7." 4
If the
.BR \-P
option is in effect, proceed to step 10. If
.BR curpath
does not begin with a
<slash>
character, set
.BR curpath
to the string formed by the concatenation of the value of
.IR PWD ,
a
<slash>
character if the value of
.IR PWD
did not end with a
<slash>
character, and
.BR curpath .
.IP " 8." 4
The
.BR curpath
value shall then be converted to canonical form as follows, considering
each component from beginning to end, in sequence:
.RS 4 
.IP " a." 4
Dot components and any
<slash>
characters that separate them from the next component shall be deleted.
.IP " b." 4
For each dot-dot component, if there is a preceding component and it is
neither root nor dot-dot, then:
.RS 4 
.IP " i." 5
If the preceding component does not refer (in the context of pathname
resolution with symbolic links followed) to a directory, then the
.IR cd
utility shall display an appropriate error message and no further steps
shall be taken.
.IP ii. 5
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.
.RE
.IP " c." 4
An implementation may further simplify
.BR 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
.BR curpath
variable is null, no further steps shall be taken.
.RE
.IP " 9." 4
If
.BR curpath
is longer than
{PATH_MAX}
bytes (including the terminating null) and the
.IR directory
operand was not longer than
{PATH_MAX}
bytes (including the terminating null), then
.BR curpath
shall be converted from an absolute pathname to an equivalent relative
pathname if possible. This conversion shall always be considered
possible if the value of
.IR PWD ,
with a trailing
<slash>
added if it does not already have one, is an initial substring of
.BR curpath .
Whether or not it is considered possible under other circumstances is
unspecified. Implementations may also apply this conversion if
.BR curpath
is not longer than
{PATH_MAX}
bytes or the
.IR directory
operand was longer than
{PATH_MAX}
bytes.
.IP 10. 4
The
.IR cd
utility shall then perform actions equivalent to the
\fIchdir\fR()
function called with
.BR curpath
as the
.IR path
argument. If these actions fail for any reason, the
.IR cd
utility shall display an appropriate error message and the remainder of
this step shall not be executed. If the
.BR \-P
option is not in effect, the
.IR PWD
environment variable shall be set to the value that
.BR curpath
had on entry to step 9 (i.e., before conversion to a relative
pathname). If the
.BR \-P
option is in effect, the
.IR PWD
environment variable shall be set to the string that would be output by
.IR pwd
.BR \-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 the
.IR PWD
environment variable is unspecified.
.P
If, during the execution of the above steps, the
.IR PWD
environment variable
is set, the
.IR 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
.IR cd ).
.SH OPTIONS
The
.IR cd
utility shall conform to the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 12.2" ", " "Utility Syntax Guidelines".
.P
The following options shall be supported by the implementation:
.IP "\fB\-L\fP" 10
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).
.IP "\fB\-P\fP" 10
Handle the operand dot-dot physically; symbolic link components shall
be resolved before dot-dot components are processed (see step 7. in the
DESCRIPTION).
.P
If both
.BR \-L
and
.BR \-P
options are specified, the last of these options shall be used and all
others ignored. If neither
.BR \-L
nor
.BR \-P
is specified, the operand shall be handled dot-dot logically; see the
DESCRIPTION.
.SH OPERANDS
The following operands shall be supported:
.IP "\fIdirectory\fR" 10
An absolute or relative pathname of the directory that shall become
the new working directory. The interpretation of a relative pathname
by
.IR cd
depends on the
.BR \-L
option and the
.IR CDPATH
and
.IR PWD
environment variables. If
.IR directory
is an empty string, the results are unspecified.
.IP "\-" 10
When a
<hyphen-minus>
is used as the operand, this shall be equivalent to the command:
.RS 10 
.sp
.RS 4
.nf

cd "$OLDPWD" && pwd
.fi
.P
.RE
.P
which changes to the previous working directory and then writes its
name.
.RE
.SH STDIN
Not used.
.SH "INPUT FILES"
None.
.SH "ENVIRONMENT VARIABLES"
The following environment variables shall affect the execution of
.IR cd :
.IP "\fICDPATH\fP" 10
A
<colon>-separated
list of pathnames that refer to directories. The
.IR cd
utility 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. If
.IR CDPATH
is not set, it shall be treated as if it were an empty string.
.IP "\fIHOME\fP" 10
The name of the directory, used when no
.IR directory
operand is specified.
.IP "\fILANG\fP" 10
Provide a default value for the internationalization variables that are
unset or null. (See the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 8.2" ", " "Internationalization Variables"
for the precedence of internationalization variables used to determine
the values of locale categories.)
.IP "\fILC_ALL\fP" 10
If set to a non-empty string value, override the values of all the
other internationalization variables.
.IP "\fILC_CTYPE\fP" 10
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).
.IP "\fILC_MESSAGES\fP" 10
.br
Determine the locale that should be used to affect the format and
contents of diagnostic messages written to standard error.
.IP "\fINLSPATH\fP" 10
Determine the location of message catalogs for the processing of
.IR LC_MESSAGES .
.IP "\fIOLDPWD\fP" 10
A pathname of the previous working directory, used by
.IR cd
.BR \- .
.IP "\fIPWD\fP" 10
This variable shall be set as specified in the DESCRIPTION. If an
application sets or unsets the value of
.IR PWD ,
the behavior of
.IR cd
is unspecified.
.SH "ASYNCHRONOUS EVENTS"
Default.
.SH STDOUT
If a non-empty directory name from
.IR CDPATH
is used, or if
.IR cd
.BR \-
is used, an absolute pathname of the new working directory shall be
written to the standard output as follows:
.sp
.RS 4
.nf

"%s\en", <\fInew directory\fR>
.fi
.P
.RE
.P
Otherwise, there shall be no output.
.SH STDERR
The standard error shall be used only for diagnostic messages.
.SH "OUTPUT FILES"
None.
.SH "EXTENDED DESCRIPTION"
None.
.SH "EXIT STATUS"
The following exit values shall be returned:
.IP "\00" 6
The directory was successfully changed.
.IP >0 6
An error occurred.
.SH "CONSEQUENCES OF ERRORS"
The working directory shall remain unchanged.
.LP
.IR "The following sections are informative."
.SH "APPLICATION USAGE"
Since
.IR 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:
.sp
.RS 4
.nf

(cd /tmp)
nohup cd
find . -exec cd {} \e;
.fi
.P
.RE
.P
it does not affect the working directory of the caller's environment.
.P
The user must have execute (search) permission in
.IR directory
in order to change to it.
.SH EXAMPLES
The following template can be used to perform processing in the directory
specified by
.IR location
and end up in the current working directory in use before the first
.IR cd
command was issued:
.sp
.RS 4
.nf

cd \fIlocation\fP
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 -
.fi
.P
.RE
.SH RATIONALE
The use of the
.IR CDPATH
was introduced in the System V shell. Its use is analogous to the use
of the
.IR PATH
variable in the shell. The BSD C shell used a shell parameter
.IR cdpath
for this purpose.
.P
A common extension when
.IR HOME
is undefined is to get the login directory from the user database for
the invoking user. This does not occur on System V implementations.
.P
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
.BR '/' 
in the pathname, remembering what the user typed, rather than
performing the equivalent of:
.sp
.RS 4
.nf

chdir("..");
.fi
.P
.RE
.P
In such a shell, the following commands would not necessarily produce
equivalent output for all directories:
.sp
.RS 4
.nf

cd .. && ls      ls ..
.fi
.P
.RE
.P
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 this functionality. The logical
pathname is stored in the
.IR PWD
environment variable when the
.IR cd
utility completes and this value is used to construct the next
directory name if
.IR cd
is invoked with the
.BR \-L
option.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "Section 2.12" ", " "Shell Execution Environment",
.IR "\fIpwd\fR\^"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "Chapter 8" ", " "Environment Variables",
.IR "Section 12.2" ", " "Utility Syntax Guidelines"
.P
The System Interfaces volume of POSIX.1\(hy2017,
.IR "\fIchdir\fR\^(\|)"
.\"
.SH 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 .
.PP
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 .