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:
- If string is //, it is implementation-dependent whether steps 2 to 5 are skipped or processed.
- If string consists entirely of slash characters, string will be set to a single slash character. In this case, skip steps 3 to 5.
- If there are any trailing slash characters in string, they will 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 will 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 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.
None.
The following operands are supported:
- string
- A string.
- suffix
- A string.
Not used.
None.
The following environment variables affect the execution of basename:
- LANG
- 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.
- LC_ALL
- If set to a non-empty string value, override the values of all the other internationalisation variables.
- LC_CTYPE
- 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).
- LC_MESSAGES
- Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
- NLSPATH
- Determine the location of message catalogues for the processing of LC_MESSAGES .
Default.
The
basename
utility will write a line to the standard output
in the following format:
"%s\n", <resulting string>
Used only for diagnostic messages.
None.
None.
The following exit values are returned:
- 0
- Successful completion.
- >0
- An error occurred.
Default.
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:produces a filename that could be used to open the file named by string in the directory returned by:$(basename "string")
$(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)
None.
dirname,Parameters and Variables .