The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


basename - return non-directory portion of a pathname


basename string [suffix]


The string operand will be treated as a pathname, as defined in Pathname. The string string will be converted to the filename corresponding to the last pathname component in string and then the suffix string suffix, if present, will be removed. This will be done by performing actions equivalent to the following steps in order:

  1. If string is //, it is implementation-dependent whether steps 2 to 5 are skipped or processed.

  2. If string consists entirely of slash characters, string will be set to a single slash character. In this case, skip steps 3 to 5.

  3. If there are any trailing slash characters in string, they will be removed.

  4. If there are any slash characters remaining in string, the prefix of string up to and including the last slash character in string will be removed.

  5. 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 will be removed from string. Otherwise, string will not be modified by this step. It will not be considered an error if suffix is not found in string.

The resulting string will be written to standard output.




The following operands are supported:
A string.
A string.


Not used.




The following environment variables affect the execution of basename:
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
If set to a non-empty string value, override the values of all the other internationalisation variables.
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
Determine the location of message catalogues for the processing of LC_MESSAGES .




The basename utility will write a line to the standard output in the following format:

"%s\n", <resulting string>


Used only for diagnostic messages.






The following exit values are returned:
Successful completion.
An error occurred.




The definition of pathname specifies implementation-dependent behaviour for pathnames starting with two slash characters. Therefore, applications must not arbitrarily add slashes to the beginning of a pathname unless they can ensure that there are more or less than two or are prepared to deal with the implementation-dependent consequences.


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)




dirname, Parameters and Variables .

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]