utility

basename

Print the last component of a pathname; optional suffix removal. Does not check existence. GNU supports --.

Formal Syntax & Structure

bash syntaxbash
basename [OPTION] NAME [SUFFIX]

Parameters & Attributes

NameTypeDescription
pathstringFile or path operand; quote shell expansions.

Return Value

exit status

Exit status 0 on success; nonzero indicates failure or a command-specific condition. See description and example.

Working Example

Example Snippetbash
path='reports/daily report.txt'basename -- "$path"dirname -- "$path"
Bash Reference