Returns the base file name of a string parameter.
basename String [ Suffix ]
The basename command reads the String parameter, deletes any prefix that ends with a / (slash) and any specified Suffix parameter, and writes the remaining base file name to standard output. The basename command applies the following rules in creating the base file name:
K > basename /u/dee/desktop/cns.boo cns.boo
cns.boo
If a Suffix parameter is specified and is not identical to all the characters in the string but is identical to a suffix in the string, the specified suffix is removed. For example, entering:
K > basename /u/dee/desktop/cns.boo .boo
cns
Failure to find the specified suffix within a string is not considered an error.
The basename and dirname commands are generally used inside command substitutions within a shell script to specify an output file name that is some variation of a specified input file name.
This command returns the following
exit values:
0 | Successful completion. |
>0 | An error occurred. |
basename $WORKFILE
The 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, then the command displays program.c.
OFILE=`basename $1 .c`.o
This command assigns to the OFILE file the value of the first positional parameter ($1), but with its .c suffix changed to .o. If $1 is the /home/jim/program.c file, OFILE becomes program.o. Because program.o is only a base file name, it identifies a file in the current directory.
Note: The ` (grave accent) specifies command substitution.
/usr/bin/basename | Contains the basename command. |
The dirname command, sh command.