basename - return file-name portion of path name


basename string [suffix]


The basename(1) utility treats the string argument as a path name. The string is converted to the file name corresponding to the last path-name component in string and then the suffix string, if present, is removed. The basename utility does this by performing actions equivalent to the following steps in order:

  1. If string consists entirely of slash (/) characters, string is set to a single slash character, and the remaining steps are skipped.
  2. If there are any trailing slash characters in string, they are removed.
  3. If there are any slash characters remaining in string, the prefix of string up to and including the last slash character in string is removed.
  4. 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 string is removed from the end of string. Otherwise, string is not modified by this step. It is not considered an error if suffix is not found in string.

The resulting string is written to standard output.

The basename(1) utility exits 0 on success, and >0 if an error occurs.