[shell] Trimming fixed prefixes and suffixes from string variables
In shell scripting, there are often cases where you want to remove file extensions like `.txt` or `.png` from a string variable. In Python, you could use the `split` method, but how can you handle this simply in a shell script?
When writing shell scripts, you often encounter situations where you need to remove file extensions like .txt
or .png
from filenames or eliminate specific prefixes. For example, you might want to extract just the filename from readme.txt
or remove a prefix like revised_
from revised_file1.txt
. In this post, we’ll explore how to use shell parameter expansion to easily trim fixed prefixes and suffixes from string variables.
What is Shell Parameter Expansion?
Shell parameter expansion is a powerful feature in shell scripting that allows you to manipulate variable values or extract substrings. It simplifies tasks like removing parts of a string or trimming specific patterns, especially when dealing with prefixes or suffixes. For a detailed overview of the various types and uses of shell parameter expansion, you can refer to this page.
Trimming Fixed Prefixes and Suffixes Using Shell Parameter Expansion
1. Trimming Suffixes (Removing Extensions): Using %
To remove a file extension, such as .txt
from file.txt
, you can use the following approach:
filename="file.txt"
name_without_extension="${filename%.*}" # Removes .txt
echo $name_without_extension # Output: file
Here, ${filename%.*}
uses the %
operator to remove the shortest matching pattern from the end of the string.
If you want to remove all extensions (e.g., from archive.tar.gz
), you can use %%
instead, which removes the longest matching pattern:
filename="archive.tar.gz"
name_without_extension="${filename%.*}"
echo $name_without_extension # Output: archive.tar
name_without_all_extensions="${filename%%.*}"
echo $name_without_all_extensions # Output: archive
2. Trimming Prefixes: Using #
To remove a fixed prefix, such as image_
from image_photo.png
, you can use the following:
filename="image_photo.png"
name_without_prefix="${filename#image_}"
echo $name_without_prefix # Output: photo.png
Here, ${filename#image_}
uses the #
operator to remove the shortest matching pattern from the beginning of the string.
Similarly, you can use ##
to remove the longest matching pattern:
filename="backup_2023_10_01.tar.gz"
name_without_prefix="${filename#backup_}"
echo $name_without_prefix # Output: 2023_10_01.tar.gz
3. Trimming Both Prefixes and Suffixes
If you need to remove both a prefix and a suffix, you can combine the two expansion techniques:
filename="prefix_file_suffix.txt"
name_without_prefix_suffix="${filename#prefix_}"
name_without_prefix_suffix="${name_without_prefix_suffix%_suffix.txt}"
echo $name_without_prefix_suffix # Output: file
Conclusion
In this post, we explored how to use shell parameter expansion to trim fixed prefixes and suffixes from string variables. To summarize:
- Use
${variable%pattern}
to remove a suffix. - Use
${variable#pattern}
to remove a prefix.
These techniques are incredibly useful for simplifying tasks like file extension removal or pattern-based string manipulation in shell scripts. While it’s easy to forget these handy features, incorporating them into your scripts can make your code more concise and efficient. Let’s make sure to remember them for future use! 😊