DIRNAME(1) General Commands Manual DIRNAME(1)

dirnamereturn directory portion of pathname

dirname string

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:

  1. If string is //, skip steps (2) through (5).
  2. If string consists entirely of slash characters, string shall be set to a single slash character. In this case, skip steps (3) through (8).
  3. If there are any trailing slash characters in string, they shall be removed.
  4. 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).
  5. If there are any trailing nonslash characters in string, they shall be removed.
  6. If the remaining string is //, it is implementation defined whether steps
  7. and (8) are skipped or processed.
  8. If there are any trailing slash characters in string, they shall be removed.
  9. If the remaining string is empty, string shall be set to a single slash character.

The resulting string shall be written to standard output.

None.

The following operand shall be supported by the implementation:

string

A string.

None.

None.

The following environment variables shall affect the execution of dirname:

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.

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_.

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).

This variable shall determine the language in which messages should be written.

Default.

The dirname utility shall write a line to the standard output in the following format: "%s\n", <resulting string>

Used only for diagnostic messages.

None.

None.

The dirname utility shall exit with one of the following values:

Successful completion.

An error occurred.

Default.

The dirname utility originated in System . 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

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 . 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.

September 1991 posix.fail