I’m iterating over all the *.info files in a directory, and checking to see if there is a corresponding *.gz file (and removing the .info file if there isn’t). But to do this, I need to strip the .info off of $file. I’ve fiddled around with parameter substitution in bash but nothing seems to work. I can get the extension easily enough (as the code below does) but not everything but the extension. I tried various things I expected might work; none did.
for file in *.info; do
gz_file="${file:(-4)}" # wrong... what is right?
# gz_file=`echo "$file" | sed -e 's/.info//'` # easy alternative I'm trying to avoid
if [[ ! -f "${gz_file}" ]]; then
rm "${file}"
fi
done
I realize this can easily be done with sed, as shown in the commented-out line, but I’d like to do it with the variable manipulation capabilities built into bash, if that’s possible, because I think it’ll be handy to have figured out how to make it work that way.
Did you try
${file%.*}?This should remove the extension.