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
11
13 basename — return non-directory portion of a pathname
14
16 basename string [suffix]
17
19 The string operand shall be treated as a pathname, as defined in the
20 Base Definitions volume of POSIX.1‐2008, Section 3.267, Pathname. The
21 string string shall be converted to the filename corresponding to the
22 last pathname component in string and then the suffix string suffix, if
23 present, shall be removed. This shall be done by performing actions
24 equivalent to the following steps in order:
25
26 1. If string is a null string, it is unspecified whether the resulting
27 string is '.' or a null string. In either case, skip steps 2
28 through 6.
29
30 2. If string is "//", it is implementation-defined whether steps 3 to
31 6 are skipped or processed.
32
33 3. If string consists entirely of <slash> characters, string shall be
34 set to a single <slash> character. In this case, skip steps 4 to 6.
35
36 4. If there are any trailing <slash> characters in string, they shall
37 be removed.
38
39 5. If there are any <slash> characters remaining in string, the prefix
40 of string up to and including the last <slash> character in string
41 shall be removed.
42
43 6. If the suffix operand is present, is not identical to the charac‐
44 ters remaining in string, and is identical to a suffix of the char‐
45 acters remaining in string, the suffix suffix shall be removed from
46 string. Otherwise, string is not modified by this step. It shall
47 not be considered an error if suffix is not found in string.
48
49 The resulting string shall be written to standard output.
50
52 None.
53
55 The following operands shall be supported:
56
57 string A string.
58
59 suffix A string.
60
62 Not used.
63
65 None.
66
68 The following environment variables shall affect the execution of base‐
69 name:
70
71 LANG Provide a default value for the internationalization vari‐
72 ables that are unset or null. (See the Base Definitions vol‐
73 ume of POSIX.1‐2008, Section 8.2, Internationalization Vari‐
74 ables for the precedence of internationalization variables
75 used to determine the values of locale categories.)
76
77 LC_ALL If set to a non-empty string value, override the values of
78 all the other internationalization variables.
79
80 LC_CTYPE Determine the locale for the interpretation of sequences of
81 bytes of text data as characters (for example, single-byte as
82 opposed to multi-byte characters in arguments).
83
84 LC_MESSAGES
85 Determine the locale that should be used to affect the format
86 and contents of diagnostic messages written to standard
87 error.
88
89 NLSPATH Determine the location of message catalogs for the processing
90 of LC_MESSAGES.
91
93 Default.
94
96 The basename utility shall write a line to the standard output in the
97 following format:
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 $(basename -- "string")
133
134 produces a filename that could be used to open the file named by string
135 in the directory returned by:
136
137 $(dirname -- "string")
138
139 If the string string is not a valid pathname, the same algorithm is
140 used, but the result need not be a valid filename. The basename utility
141 is not expected to make any judgements about the validity of string as
142 a pathname; it just follows the specified algorithm to produce a result
143 string.
144
145 The following shell script compiles /usr/src/cmd/cat.c and moves the
146 output to a file named cat in the current directory when invoked with
147 the argument /usr/src/cmd/cat or with the argument /usr/src/cmd/cat.c:
148
149 c99 -- "$(dirname -- "$1")/$(basename -- "$1" .c).c" &&
150 mv a.out "$(basename -- "$1" .c)"
151
153 The behaviors of basename and dirname have been coordinated so that
154 when string is a valid pathname:
155
156 $(basename -- "string")
157
158 would be a valid filename for the file in the directory:
159
160 $(dirname -- "string")
161
162 This would not work for the early proposal versions of these utilities
163 due to the way it specified handling of trailing <slash> characters.
164
165 Since the definition of pathname specifies implementation-defined
166 behavior for pathnames starting with two <slash> characters, this vol‐
167 ume of POSIX.1‐2008 specifies similar implementation-defined behavior
168 for the basename and dirname utilities.
169
171 None.
172
174 Section 2.5, Parameters and Variables, dirname
175
176 The Base Definitions volume of POSIX.1‐2008, Section 3.267, Pathname,
177 Chapter 8, Environment Variables
178
180 Portions of this text are reprinted and reproduced in electronic form
181 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
182 -- Portable Operating System Interface (POSIX), The Open Group Base
183 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
184 cal and Electronics Engineers, Inc and The Open Group. (This is
185 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
186 event of any discrepancy between this version and the original IEEE and
187 The Open Group Standard, the original IEEE and The Open Group Standard
188 is the referee document. The original Standard can be obtained online
189 at http://www.unix.org/online.html .
190
191 Any typographical or formatting errors that appear in this page are
192 most likely to have been introduced during the conversion of the source
193 files to man page format. To report such errors, see https://www.ker‐
194 nel.org/doc/man-pages/reporting_bugs.html .
195
196
197
198IEEE/The Open Group 2013 BASENAME(1P)