BASH Programming

How to Extract File Basename Without Path and Extension in Bash

The basename command is commonly used in Bash to retrieve the base name of a file or directory from a given path by removing any leading directory components. This article will explore how to extract the base name of a file without its path and extension using bash.

Extract File Basename Without Path and Extension in Bash

To extract the basename of a file without its path and extension, we can use the basename command in conjunction with the parameter substitution feature of bash. The basename command returns the last component of a pathname, which in our case would be the file name with its extension. However, by specifying the suffix option, we can strip the extension from the file name, here’s an example bash code:

#!bin/bash
filepath=/home/aaliyan/bash3.sh
s=$(basename $filepath)
echo "${s%.*}"

The above bash script defines a variable called “filepath” and assigns it the path of the file “/home/aaliyan/bash3.sh“. The script then uses the basename command to extract the base name of the file from the file path and assigns the result to a variable called “s”.

The second parameter expansion removes the extension from the file name by removing the shortest possible match of any number of characters followed by a dot using “%.*”. The resulting string, “bash3”, is then printed to the console using the echo command:

Another way to extract the basename of a file without its file path and extension is by using the parameter expansion that is without using the basename command, below is the example bash code that uses the parameter expansion method to get the basename of a file without file path and file extension:

#!bin/bash
filepath=/home/aaliyan/bash3.sh
s=${filepath##*/}
echo "${s%.*}"

This is a bash script that defines a variable called “filepath” and assigns it the value “/home/aaliyan/bash3.sh“. The script then uses the parameter expansion feature of bash twice to extract the basename of the file without its path and extension. Specifically, the first parameter expansion removes the path from the file name by removing the longest possible match of any number of characters followed by a forward slash using “##/”.

The resulting string, “bash3.sh” is then assigned to a variable called “s”. The second parameter expansion removes the extension from the file name by removing the shortest possible match of any number of characters followed by a dot using “%.”. The resulting string, “bash3”, is then printed to the console using the echo command:

Conclusion

Extracting the basename of a file without its path and extension is a common task in bash scripting. By using the basename command in combination with the parameter substitution and parameter expansion features of bash, we can easily achieve this task. This can be useful when working with file names in scripts, for example, when renaming files or performing operations on files with similar names.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.