BASENAME(1)

NAME
basename - return non-directory portion of a pathname

SYNOPSIS
basename string [suffix]

DESCRIPTION
The string operand is treated as a pathname. It is converted to the filename corresponding to the last pathname component in string and then the suffix string suffix , if present, is removed. This is done by performing the following actions in order:

1. Strip trailing slashes

2. If the string is empty - set it to "/", skip to 5

3. Remove the beginning of the string up to and including the last
    slash character

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

5. Write the resulting string to the standard output

OPTIONS
None. The option -- is not supported either.

OPERANDS

The following operands are supported:

string
    A string.

suffix
    A string.

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

"%s\n", <resulting string>

EXIT STATUS
The following exit values are returned:

0   Successful completion.

>0  An error occurred.

CONSEQUENCES OF ERRORS
If an error is detected, output will not be written to standard output and the exit status will not be zero.

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)

STANDARDS
The ASMIX basename utility is expected to conform to The Open Group Single Unix 2 Commands and Utilities specification.

SEE ALSO
dirname(1)