Let's say I have a string:
x=file.tar.sh
I know how to remove everything but last n-characters. Like this (removing everything but last 3 characters:
${x: -3}
But this doesn't work for files with different suffix lengths. (len .tar != len .sh)
I would tackle this by removing everything until the last dot. I've tried this:
${x##.}
This removes the longest matching until "." but somehow it just returns the full string without removing anything?
In Windows, if you delete a file extension, Windows no longer knows what to do with that file. When you try to open the file, Windows will ask you what app you want to use. If you change an extension—say you rename a file from “coolpic. jpg” to “coolpic.
More than one extension usually represents nested transformations, such as files. tar. gz (the . tar indicates that the file is a tar archive of one or more files, and the . gz indicates that the tar archive file is compressed with gzip).
You should be using the command substitution syntax $(command) when you want to execute a command in script/command. name=$(echo "$filename" | cut -f 1 -d '. ')
Try this:
x=file.tar.sh
echo ${x##*.}
This will print sh
If you want to get tar.sh
, then:
echo ${x#*.}
Here *
matches any set of characters before the occurrence of .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With