NAME
dirname — return
directory portion of pathname
SYNOPSIS
dirname |
string |
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 name of the directory containing the filename corresponding to the last pathname component in string, performing actions equivalent to the following steps in order:
- If string is //, skip steps (2) through (5).
- If string consists entirely of slash characters, string shall be set to a single slash character. In this case, skip steps (3) through (8).
- If there are any trailing slash characters in string, they shall be removed.
- If there are no slash characters remaining in string, string shall be set to a single period character. In this case, skip steps (5) through (8).
- If there are any trailing nonslash characters in string, they shall be removed.
- If the remaining string is //, it is implementation defined whether steps
- and (8) are skipped or processed.
- If there are any trailing slash characters in string, they shall be removed.
- If the remaining string is empty, string shall be set to a single slash character.
The resulting string shall be written to standard output.
OPTIONS
None.
OPERANDS
The following operand shall be supported by the implementation:
- string
-
A string.
STANDARD INPUT
None.
INPUT FILES
None.
ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of
dirname:
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 dirname 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 dirname utility shall exit with one of
the following values:
CONSEQUENCES OF ERRORS
Default.
RATIONALE
EXAMPLES
The dirname utility originated in System
III. It has
evolved through the System V releases to a version that matches the
requirements specified in this description in System V Release 3.
bc(1) BSD and earlier versions did not include
dirname. Table 4-6 indicates the results required
for some invocations of dirname.
Table 4-6 − dirname Examples
Command Results
dirname / /
dirname // / or // dirname
/a/b/ /a dirname //a//b// //a
dirname unspecified dirname
a . ($? = 0) dirname "" . ($? = 0)
dirname /a / dirname /a/b /a
dirname a/b a
HISTORY OF DECISIONS MADE
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 processing of trailing slashes was specified. Consideration was given to
leaving processing unspecified if there were trailing slashes, but this
cannot be done; the
POSIX. 1
{8} definition of pathname allows trailing slashes. The basename and
dirname utilities have to specify consistent
handling for all valid pathnames. 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.