I have a file named like this: my-file-1.2.0.jar
I want to extract the version of this file by splitting on the last -.
Hence I would have the following output: 1.2.0.jar
I would also like to get rid of the .jar if it’s possible with the same command to have this output: 1.2.0
How can I achieve that with bash ?
>Solution :
Use parameter expansion:
#! /bin/bash
filename=my-file-1.2.0.jar
version=${filename##*-}
version=${version%.jar}
echo "$version"
## removes the largest matching pattern from the left. % removes the shortest matching pattern from the right (but .jar contains no wildcards, so using %% for the longest match would work equally well).