how to replace file extension
old_name=run_fix.bash
new_name=${old_name%.bash}.in_hold.txt
printf 'New name: %s\n' "$new_name"
This would remove the filename suffix .bash
from the value of $old_name
and add .in_hold.txt
to the result of that. The whole thing would be assigned to the variable new_name
.
The expansion ${variable%pattern}
to remove the shortest suffix string matching the pattern pattern
from the value of $variable
is a standard parameter expansion.
To replace any filename suffix (i.e. anything after the last dot in the filename):
new_name=${old_name%.*}.new_suffix
The .*
pattern would match the last dot and anything after it (this would be removed). Had you used %%
instead of %
, the longest substring that matched the pattern would have been removed (in this case, you would have removed everything after the first dot in the string). If the string does not contain any dots, it remains unaltered.