Caution: This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs.

basename

Return the nondirectory portion of pathname (POSIX)

Syntax:

basename string [suffix]

Runs on:

Neutrino

Options:

string
A string of text.
suffix
A string of text.

Description:

The basename utility is useful primarily for extracting the filename portion of a pathname, but since it performs only string operations, you can use it with any string.

The basename utility prints to standard output a substring of its string argument, plus a newline character. The basename utility forms this substring by doing the following, in order:

  1. Discarding any trailing slash (/) characters.
  2. Discarding all characters up to and including the last slash character.
  3. Deleting the string argument's suffix, provided you've specified a suffix operand that's identical to the string operand's suffix.

    If suffix is equal to the remaining string, the suffix isn't removed (e.g. if suffix is prog.c and the remaining string is prog.c, the suffix .c isn't removed).

The result is a null string only if string is a null string (""). In this case, basename outputs a single newline character.

If string consists entirely of slash characters, basename prints a single slash, followed by a newline character.

You'll use the basename utility most often within shell scripts, where it's normally invoked inside back-ticks (`...`), or contained in $(...).

Examples:

Command: Output:
basename . .
basename /usr/src/prog.c prog.c
basename /usr/src/prog.c .c prog
basename /usr/src/prog.c .a prog.c
basename /usr/src/ src
basename ...//[fred] [fred]

Exit status:

0
Successful completion.
>0
An error occurred.

See also:

dirname, fullpath