Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split string on last desired character in bash

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading