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 ?
CodePudding user response:
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).
CodePudding user response:
Here is way to do this in single step using extglob
:
shopt -s extglob
s='my-file-1.2.0.jar'
echo "${s// (.jar|*-)}"
1.2.0