NAME
basename — return
nondirectory portion of pathname
SYNOPSIS
basename |
string [suffix] |
DESCRIPTION
The string operand shall be treated as a pathname, as defined in 2.2.2.102. The string string shall be converted to the filename corresponding to the last pathname component in string and then the suffix string suffix, if present, shall be removed. This shall be done by performing actions equivalent to the following steps in order:
- If string is //, it is implementation defined whether steps (2) through (5) are skipped or processed.
- If string consists entirely of slash characters, string shall be set to a single slash character. In this case, skip steps (3) through (5).
- If there are any trailing slash characters in string, they shall be removed.
- If there are any slash characters remaining in string, the prefix of string up to and including the last slash character in string shall be removed.
- If the suffix operand is present, is not identical to the characters remaining in string, and is identical to a suffix of the characters remaining in string, the suffix suffix shall be removed from string. Otherwise, string
shall not be modified by this step. It shall not be considered an error if suffix is not found in string. The resulting string shall be written to standard output.
OPTIONS
None.
OPERANDS
The following operands shall be supported by the implementation:
- string
-
A string.
- suffix
-
A string.
STANDARD INPUT
None.
INPUT FILES
None.
ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of
basename:
LANG-
This variable shall determine the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_ ) do not specify a locale. See 2.6.
LC_ALL-
This variable shall determine the locale to be used to override any values for locale categories specified by the settings of LANG or any environment variables beginning with LC_.
LC_CTYPE-
This variable shall determine the locale for the interpretation of sequences of bytes of text data as characters (e.g., single- versus multibyte characters in arguments).
LC_MESSAGES-
This variable shall determine the language in which messages should be written.
ASYNCHRONOUS EVENTS
Default.
STANDARD OUTPUT
The basename utility shall write a line to
the standard output in the following format: "%s\n", <resulting
string>
STANDARD ERROR
Used only for diagnostic messages.
OUTPUT FILES
None.
EXTENDED DESCRIPTION
None.
EXIT STATUS
The basename utility shall exit with one
of the following values:
CONSEQUENCES OF ERRORS
Default.
RATIONALE
EXAMPLES
If the string string is a valid pathname, $(
basename "string") produces a filename
that could be used to open the file named by string in the directory
returned by
$(dirname "string") If the string string is not a valid
pathname, the same algorithm is used, but the result need not be a valid
filename. The basename utility is not expected to
make any judgements about the validity of string as a pathname; it just
follows the specified algorithm to produce a result string. The following
shell script compiles /usr/src/cmd/cat.c and moves the output to a file
named cat in the current directory when invoked with the argument
/usr/src/cmd/cat or with the argument /usr/src/cmd/cat.c: c89 $(dirname
"$1")/$( basename "$1" .c).c mv
a.out $( basename "$1" .c)
HISTORY OF DECISIONS MADE
The
POSIX. 1
{8} definition of pathname allows trailing slashes on a pathname naming a
directory. Some historical implementations have not allowed trailing slashes
and thus treated pathnames of this form in other ways. Existing
implementations also differ in their handling of suffix when suffix matches
the entire string left after removing the directory part of string. The
behaviors of basename and dirname in this standard
have been coordinated so that when string is a valid pathname $(
basename "string") would be a valid
filename for the file in the directory $(dirname "string") This
would not work for the versions of these utilities in earlier drafts due to
the way it specified handling of trailing slashes. Since the definition of
pathname in 2.2.2.102 specifies implementation-defined behavior for
pathnames starting with two slash characters, Draft 11 has been changed to
specify similar implementation-defined behavior for the
basename and dirname utilities. On implementations
where the pathname // is always treated the same as the pathname /, the
functionality required by Draft 10 meets all of the Draft 11
requirements.