1DIRNAME(1P) POSIX Programmer's Manual DIRNAME(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 dirname — return the directory portion of a pathname
13
15 dirname string
16
18 The string operand shall be treated as a pathname, as defined in the
19 Base Definitions volume of POSIX.1‐2017, Section 3.271, Pathname. The
20 string string shall be converted to the name of the directory contain‐
21 ing the filename corresponding to the last pathname component in
22 string, performing actions equivalent to the following steps in order:
23
24 1. If string is //, skip steps 2 to 5.
25
26 2. If string consists entirely of <slash> characters, string shall be
27 set to a single <slash> character. In this case, skip steps 3 to 8.
28
29 3. If there are any trailing <slash> characters in string, they shall
30 be removed.
31
32 4. If there are no <slash> characters remaining in string, string
33 shall be set to a single <period> character. In this case, skip
34 steps 5 to 8.
35
36 5. If there are any trailing non-<slash> characters in string, they
37 shall be removed.
38
39 6. If the remaining string is //, it is implementation-defined whether
40 steps 7 and 8 are skipped or processed.
41
42 7. If there are any trailing <slash> characters in string, they shall
43 be removed.
44
45 8. If the remaining string is empty, string shall be set to a single
46 <slash> character.
47
48 The resulting string shall be written to standard output.
49
51 None.
52
54 The following operand shall be supported:
55
56 string A string.
57
59 Not used.
60
62 None.
63
65 The following environment variables shall affect the execution of
66 dirname:
67
68 LANG Provide a default value for the internationalization vari‐
69 ables that are unset or null. (See the Base Definitions vol‐
70 ume of POSIX.1‐2017, Section 8.2, Internationalization Vari‐
71 ables for the precedence of internationalization variables
72 used to determine the values of locale categories.)
73
74 LC_ALL If set to a non-empty string value, override the values of
75 all the other internationalization variables.
76
77 LC_CTYPE Determine the locale for the interpretation of sequences of
78 bytes of text data as characters (for example, single-byte as
79 opposed to multi-byte characters in arguments).
80
81 LC_MESSAGES
82 Determine the locale that should be used to affect the format
83 and contents of diagnostic messages written to standard
84 error.
85
86 NLSPATH Determine the location of message catalogs for the processing
87 of LC_MESSAGES.
88
90 Default.
91
93 The dirname utility shall write a line to the standard output in the
94 following format:
95
96
97 "%s\n", <resulting string>
98
100 The standard error shall be used only for diagnostic messages.
101
103 None.
104
106 None.
107
109 The following exit values shall be returned:
110
111 0 Successful completion.
112
113 >0 An error occurred.
114
116 Default.
117
118 The following sections are informative.
119
121 The definition of pathname specifies implementation-defined behavior
122 for pathnames starting with two <slash> characters. Therefore, applica‐
123 tions shall not arbitrarily add <slash> characters to the beginning of
124 a pathname unless they can ensure that there are more or less than two
125 or are prepared to deal with the implementation-defined consequences.
126
128 The EXAMPLES section of the basename() function (see the System Inter‐
129 faces volume of POSIX.1‐2017, basename()) includes a table showing
130 examples of the results of processing several sample pathnames by the
131 basename() and dirname() functions and by the basename and dirname
132 utilities.
133
134 See also the examples for the basename utility.
135
137 The behaviors of basename and dirname in this volume of POSIX.1‐2017
138 have been coordinated so that when string is a valid pathname:
139
140
141 $(basename -- "string")
142
143 would be a valid filename for the file in the directory:
144
145
146 $(dirname -- "string")
147
148 This would not work for the versions of these utilities in early pro‐
149 posals due to the way processing of trailing <slash> characters was
150 specified. Consideration was given to leaving processing unspecified if
151 there were trailing <slash> characters, but this cannot be done; the
152 Base Definitions volume of POSIX.1‐2017, Section 3.271, Pathname allows
153 trailing <slash> characters. The basename and dirname utilities have to
154 specify consistent handling for all valid pathnames.
155
157 None.
158
160 Section 2.5, Parameters and Variables, basename
161
162 The Base Definitions volume of POSIX.1‐2017, Section 3.271, Pathname,
163 Chapter 8, Environment Variables
164
165 The System Interfaces volume of POSIX.1‐2017, basename(), dirname()
166
168 Portions of this text are reprinted and reproduced in electronic form
169 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
170 table Operating System Interface (POSIX), The Open Group Base Specifi‐
171 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
172 Electrical and Electronics Engineers, Inc and The Open Group. In the
173 event of any discrepancy between this version and the original IEEE and
174 The Open Group Standard, the original IEEE and The Open Group Standard
175 is the referee document. The original Standard can be obtained online
176 at http://www.opengroup.org/unix/online.html .
177
178 Any typographical or formatting errors that appear in this page are
179 most likely to have been introduced during the conversion of the source
180 files to man page format. To report such errors, see https://www.ker‐
181 nel.org/doc/man-pages/reporting_bugs.html .
182
183
184
185IEEE/The Open Group 2017 DIRNAME(1P)