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

NAME

6       basename - return non-directory portion of a pathname
7

SYNOPSIS

9       basename string [suffix]
10

DESCRIPTION

12       The  string  operand  shall be treated as a pathname, as defined in the
13       Base Definitions volume of IEEE Std 1003.1-2001, Section  3.266,  Path‐
14       name.  The string string shall be converted to the filename correspond‐
15       ing to the last pathname component in string and then the suffix string
16       suffix,  if present, shall be removed. This shall be done by performing
17       actions equivalent to the following steps in order:
18
19        1. If string is a null string, it is unspecified whether the resulting
20           string  is  '.'  or  a  null  string.  In either case, skip steps 2
21           through 6.
22
23        2. If string is "//" , it is implementation-defined whether steps 3 to
24           6 are skipped or processed.
25
26        3. If  string  consists  entirely of slash characters, string shall be
27           set to a single slash character. In this case, skip steps 4 to 6.
28
29        4. If there are any trailing slash characters in string, they shall be
30           removed.
31
32        5. If  there  are any slash characters remaining in string, the prefix
33           of string up to and including the last slash  character  in  string
34           shall be removed.
35
36        6. If  the  suffix operand is present, is not identical to the charac‐
37           ters remaining in string, and is identical to a suffix of the char‐
38           acters remaining in string, the suffix suffix shall be removed from
39           string. Otherwise, string is not modified by this  step.  It  shall
40           not be considered an error if suffix is not found in string.
41
42       The resulting string shall be written to standard output.
43

OPTIONS

45       None.
46

OPERANDS

48       The following operands shall be supported:
49
50       string A string.
51
52       suffix A string.
53
54

STDIN

56       Not used.
57

INPUT FILES

59       None.
60

ENVIRONMENT VARIABLES

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

ASYNCHRONOUS EVENTS

89       Default.
90

STDOUT

92       The  basename  utility shall write a line to the standard output in the
93       following format:
94
95
96              "%s\n", <resulting string>
97

STDERR

99       The standard error shall be used only for diagnostic messages.
100

OUTPUT FILES

102       None.
103

EXTENDED DESCRIPTION

105       None.
106

EXIT STATUS

108       The following exit values shall be returned:
109
110        0     Successful completion.
111
112       >0     An error occurred.
113
114

CONSEQUENCES OF ERRORS

116       Default.
117
118       The following sections are informative.
119

APPLICATION USAGE

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 slashes to the beginning of a  pathname
124       unless they can ensure that there are more or less than two or are pre‐
125       pared to deal with the implementation-defined consequences.
126

EXAMPLES

128       If the string string is a valid pathname:
129
130
131              $(basename "string")
132
133       produces a filename that could be used to open the file named by string
134       in the directory returned by:
135
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  util‐
141       ity is not expected to make any judgements about the validity of string
142       as a pathname; it just follows the specified  algorithm  to  produce  a
143       result 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
150              c99 $(dirname "$1")/$(basename "$1" .c).c
151              mv a.out $(basename "$1" .c)
152

RATIONALE

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

FUTURE DIRECTIONS

174       None.
175

SEE ALSO

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