basename

Print the base name of a directory or file

Supplementary instructions

basename command is used to print the base name of a directory or file. The basename and dirname commands are often used for command substitution in shell scripts to specify an output file name that is different from the specified input file name.

grammar

basename(option)(parameter)

Options

--help: display help;
--version: Display version number.

Parameters

Example

basename $WORKFILE

This command displays the base name of the value assigned to the shell variable WORKFILE. If the value of the WORKFILE variable is the /home/jim/program.c file, this command displays program.c.

To construct a file name that is the same as another file name (except for the suffix), enter:

OFILE=`basename $1 .c`.o

This command assigns the value of the argument ($1) in the first position of the OFILE file, but changes its .c suffix to .o. If $1 is the file /home/jim/program.c, OFILE becomes program.o. Because program.o is just a base file name, it identifies the file in the current directory.