Return nondirectory portion of pathname (POSIX)
basename string [suffix]
The basename utility is useful primarily for extracting the filename portion of a pathname, but since it performs only string operations, it can be used 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:
If suffix is equal to the remaining string, the suffix won't be removed (e.g. if suffix is prog.c and the remaining string is prog.c, the suffix .c isn't removed).
The result may be 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-tick (`...`), or contained in $(...).
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] |
The basename utility writes the basename string derived from the command-line parameters to its standard output.