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, 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:
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 $(...).
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] |