1BASENAME(1P)               POSIX Programmer's Manual              BASENAME(1P)
2
3
4

PROLOG

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

NAME

12       basename - return non-directory portion of a pathname
13

SYNOPSIS

15       basename string [suffix]
16

DESCRIPTION

18       The string operand shall be treated as a pathname, as  defined  in  the
19       Base  Definitions  volume of IEEE Std 1003.1-2001, Section 3.266, Path‐
20       name. The string string shall be converted to the filename  correspond‐
21       ing to the last pathname component in string and then the suffix string
22       suffix, if present, shall be removed. This shall be done by  performing
23       actions 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 be
36           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

OPTIONS

51       None.
52

OPERANDS

54       The following operands shall be supported:
55
56       string A string.
57
58       suffix A string.
59
60

STDIN

62       Not used.
63

INPUT FILES

65       None.
66

ENVIRONMENT VARIABLES

68       The following environment variables shall affect the execution of base‐
69       name:
70
71       LANG   Provide  a  default value for the internationalization variables
72              that are unset or null. (See  the  Base  Definitions  volume  of
73              IEEE Std 1003.1-2001,  Section  8.2,  Internationalization Vari‐
74              ables for the precedence of internationalization variables  used
75              to determine the values of locale categories.)
76
77       LC_ALL If  set  to a non-empty string value, override the values of all
78              the other internationalization variables.
79
80       LC_CTYPE
81              Determine the locale for  the  interpretation  of  sequences  of
82              bytes  of  text  data as characters (for example, single-byte as
83              opposed to multi-byte characters in arguments).
84
85       LC_MESSAGES
86              Determine the locale that should be used to  affect  the  format
87              and contents of diagnostic messages written to standard error.
88
89       NLSPATH
90              Determine the location of message catalogs for the processing of
91              LC_MESSAGES .
92
93

ASYNCHRONOUS EVENTS

95       Default.
96

STDOUT

98       The basename utility shall write a line to the standard output  in  the
99       following format:
100
101
102              "%s\n", <resulting string>
103

STDERR

105       The standard error shall be used only for diagnostic messages.
106

OUTPUT FILES

108       None.
109

EXTENDED DESCRIPTION

111       None.
112

EXIT STATUS

114       The following exit values shall be returned:
115
116        0     Successful completion.
117
118       >0     An error occurred.
119
120

CONSEQUENCES OF ERRORS

122       Default.
123
124       The following sections are informative.
125

APPLICATION USAGE

127       The  definition  of  pathname specifies implementation-defined behavior
128       for pathnames starting with two slash characters.  Therefore,  applica‐
129       tions  shall not arbitrarily add slashes to the beginning of a pathname
130       unless they can ensure that there are more or less than two or are pre‐
131       pared to deal with the implementation-defined consequences.
132

EXAMPLES

134       If the string string is a valid pathname:
135
136
137              $(basename "string")
138
139       produces a filename that could be used to open the file named by string
140       in the directory returned by:
141
142
143              $(dirname "string")
144
145       If the string string is not a valid pathname,  the  same  algorithm  is
146       used,  but the result need not be a valid filename.  The basename util‐
147       ity is not expected to make any judgements about the validity of string
148       as  a  pathname;  it  just follows the specified algorithm to produce a
149       result string.
150
151       The following shell script compiles /usr/src/cmd/cat.c  and  moves  the
152       output  to  a file named cat in the current directory when invoked with
153       the argument /usr/src/cmd/cat or with the argument /usr/src/cmd/cat.c:
154
155
156              c99 $(dirname "$1")/$(basename "$1" .c).c
157              mv a.out $(basename "$1" .c)
158

RATIONALE

160       The behaviors of basename and dirname have  been  coordinated  so  that
161       when string is a valid pathname:
162
163
164              $(basename "string")
165
166       would be a valid filename for the file in the directory:
167
168
169              $(dirname "string")
170
171       This  would not work for the early proposal versions of these utilities
172       due to the way it specified handling of trailing slashes.
173
174       Since  the  definition  of  pathname  specifies  implementation-defined
175       behavior  for pathnames starting with two slash characters, this volume
176       of IEEE Std 1003.1-2001 specifies similar implementation-defined behav‐
177       ior for the basename and dirname utilities.
178

FUTURE DIRECTIONS

180       None.
181

SEE ALSO

183       Parameters and Variables, dirname()
184
186       Portions  of  this text are reprinted and reproduced in electronic form
187       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
188       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
189       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
190       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
191       event of any discrepancy between this version and the original IEEE and
192       The  Open Group Standard, the original IEEE and The Open Group Standard
193       is the referee document. The original Standard can be  obtained  online
194       at http://www.opengroup.org/unix/online.html .
195
196
197
198IEEE/The Open Group                  2003                         BASENAME(1P)
Impressum