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

 NAME

basename - return non-directory portion of a pathname

 SYNOPSIS



basename string [suffix]

 DESCRIPTION

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.

 OPTIONS

None.

 OPERANDS

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

 STDIN

Not used.

 INPUT FILES

None.

 ENVIRONMENT VARIABLES

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 .

 ASYNCHRONOUS EVENTS

Default.

 STDOUT

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

"%s\n", <resulting string>

 STDERR

Used only for diagnostic messages.

 OUTPUT FILES

None.

 EXTENDED DESCRIPTION

None.

 EXIT STATUS

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

 CONSEQUENCES OF ERRORS

Default.

 APPLICATION USAGE

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.

 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)

 FUTURE DIRECTIONS

None.

 SEE ALSO

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 ]