1BASENAME(1P) POSIX Programmer's Manual BASENAME(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 basename — return non-directory portion of a pathname
13
15 basename string [suffix]
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 filename corresponding to the
21 last pathname component in string and then the suffix string suffix, if
22 present, shall be removed. This shall be done by performing actions
23 equivalent to the following steps in order:
24
25 1. If string is a null string, it is unspecified whether the resulting
26 string is '.' or a null string. In either case, skip steps 2
27 through 6.
28
29 2. If string is "//", it is implementation-defined whether steps 3 to
30 6 are skipped or processed.
31
32 3. If string consists entirely of <slash> characters, string shall be
33 set to a single <slash> character. In this case, skip steps 4 to 6.
34
35 4. If there are any trailing <slash> characters in string, they shall
36 be removed.
37
38 5. If there are any <slash> characters remaining in string, the prefix
39 of string up to and including the last <slash> character in string
40 shall be removed.
41
42 6. If the suffix operand is present, is not identical to the charac‐
43 ters remaining in string, and is identical to a suffix of the char‐
44 acters remaining in string, the suffix suffix shall be removed from
45 string. Otherwise, string is not modified by this step. It shall
46 not be considered an error if suffix is not found in string.
47
48 The resulting string shall be written to standard output.
49
51 None.
52
54 The following operands shall be supported:
55
56 string A string.
57
58 suffix A string.
59
61 Not used.
62
64 None.
65
67 The following environment variables shall affect the execution of base‐
68 name:
69
70 LANG Provide a default value for the internationalization vari‐
71 ables that are unset or null. (See the Base Definitions vol‐
72 ume of POSIX.1‐2017, Section 8.2, Internationalization Vari‐
73 ables for the precedence of internationalization variables
74 used to determine the values of locale categories.)
75
76 LC_ALL If set to a non-empty string value, override the values of
77 all the other internationalization variables.
78
79 LC_CTYPE Determine the locale for the interpretation of sequences of
80 bytes of text data as characters (for example, single-byte as
81 opposed to multi-byte characters in arguments).
82
83 LC_MESSAGES
84 Determine the locale that should be used to affect the format
85 and contents of diagnostic messages written to standard
86 error.
87
88 NLSPATH Determine the location of message catalogs for the processing
89 of LC_MESSAGES.
90
92 Default.
93
95 The basename utility shall write a line to the standard output in the
96 following format:
97
98
99 "%s\n", <resulting string>
100
102 The standard error shall be used only for diagnostic messages.
103
105 None.
106
108 None.
109
111 The following exit values shall be returned:
112
113 0 Successful completion.
114
115 >0 An error occurred.
116
118 Default.
119
120 The following sections are informative.
121
123 The definition of pathname specifies implementation-defined behavior
124 for pathnames starting with two <slash> characters. Therefore, applica‐
125 tions shall not arbitrarily add <slash> characters to the beginning of
126 a pathname unless they can ensure that there are more or less than two
127 or are prepared to deal with the implementation-defined consequences.
128
130 If the string string is a valid pathname:
131
132
133 $(basename -- "string")
134
135 produces a filename that could be used to open the file named by string
136 in the directory returned by:
137
138
139 $(dirname -- "string")
140
141 If the string string is not a valid pathname, the same algorithm is
142 used, but the result need not be a valid filename. The basename utility
143 is not expected to make any judgements about the validity of string as
144 a pathname; it just follows the specified algorithm to produce a result
145 string.
146
147 The following shell script compiles /usr/src/cmd/cat.c and moves the
148 output to a file named cat in the current directory when invoked with
149 the argument /usr/src/cmd/cat or with the argument /usr/src/cmd/cat.c:
150
151
152 c99 -- "$(dirname -- "$1")/$(basename -- "$1" .c).c" &&
153 mv a.out "$(basename -- "$1" .c)"
154
155 The EXAMPLES section of the basename() function (see the System Inter‐
156 faces volume of POSIX.1‐2017, basename()) includes a table showing
157 examples of the results of processing several sample pathnames by the
158 basename() and dirname() functions and by the basename and dirname
159 utilities.
160
162 The behaviors of basename and dirname have been coordinated so that
163 when string is a valid pathname:
164
165
166 $(basename -- "string")
167
168 would be a valid filename for the file in the directory:
169
170
171 $(dirname -- "string")
172
173 This would not work for the early proposal versions of these utilities
174 due to the way it specified handling of trailing <slash> characters.
175
176 Since the definition of pathname specifies implementation-defined
177 behavior for pathnames starting with two <slash> characters, this vol‐
178 ume of POSIX.1‐2017 specifies similar implementation-defined behavior
179 for the basename and dirname utilities.
180
182 None.
183
185 Section 2.5, Parameters and Variables, dirname
186
187 The Base Definitions volume of POSIX.1‐2017, Section 3.271, Pathname,
188 Chapter 8, Environment Variables
189
190 The System Interfaces volume of POSIX.1‐2017, basename(), dirname()
191
193 Portions of this text are reprinted and reproduced in electronic form
194 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
195 table Operating System Interface (POSIX), The Open Group Base Specifi‐
196 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
197 Electrical and Electronics Engineers, Inc and The Open Group. In the
198 event of any discrepancy between this version and the original IEEE and
199 The Open Group Standard, the original IEEE and The Open Group Standard
200 is the referee document. The original Standard can be obtained online
201 at http://www.opengroup.org/unix/online.html .
202
203 Any typographical or formatting errors that appear in this page are
204 most likely to have been introduced during the conversion of the source
205 files to man page format. To report such errors, see https://www.ker‐
206 nel.org/doc/man-pages/reporting_bugs.html .
207
208
209
210IEEE/The Open Group 2017 BASENAME(1P)