Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

basename - return non-directory portion of path name

&pagelevel(4)&pagelevel

You can use basename to

  • extract the basic file name (basename) from the full path name,

  • strip any suffixes from the file name.

basename strips all characters up to and including the last / from the specified string and
writes the result to standard output. The basic file name can thus be separated from its path
prefix. If you also specify a string suffix as a command-line argument, basename will strip this
suffix as well. basename is useful in shell scripts.


Syntax


basename[ string[ suffix]]

string

string can be any character string.

basename deletes all characters up to and including the last / from string and writes the result to standard output. Strings that do not include a slash are output unmodified.

string not specified:
A period (dot) is written to standard output.

suffix

suffix can be any character string.
If the specified suffix matches the end of string, string is output without suffix.

Locale

The following environment variables affect the execution of basename:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

The name prog is to be generated from /home/catherine/program:

$ basename /home/catherine/program ramm
prog


Example 2

The following shell script compiles a C source program. basename generates the name of the compiled program from the file name used as a command-line argument for the shell script. The compiled program is stored in an executable file in the current working directory. The shell script is called compile.

Contents of compile:

c89 -o ’basename $1 .c’ $1

If you call compile as follows:

$ compile /home/anna/cprogs/tab.c

then the name of the C source file is passed to the command c89 (control program for the compiling and linking of C programs, see c89 [5]) in the positional parameter $1.

The shell replaces the operand for the c89 option -o with the result of the basename call. The name of the executable file is tab.

See also

dirname, ed