Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm using the bash shell in CentOS, version ...

$ uname -a
Linux 3.2.29-48.63.amzn1.x86_64 #1 SMP Sat Sep 15 01:43:12 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

In my shell script, I want to get part of a string, specifically, everything before the last dash ("-"). So if my string is "my-importer-1.0.ear," I want to extract "my-importer". However, the expression below


is only returning "my", because its matching against the first dash. How can I adjust the expression to match everything up until the last dash?

share|improve this question
Use sed instead? echo my-importer-1.0.ear | sed -re 's/^(.*)-.*$/\1/' – Zoredache Nov 1 '12 at 19:28

1 Answer 1

up vote 1 down vote accepted

Are you sure you're not using %% which deletes the longest match from the end of the string and thus gives you my whereas a % deletes the shortest match and provides (for you example) what you want

echo ${fname%%-*}


echo ${fname%-*}
share|improve this answer
You're right ... I got confused. Thanks, – Dave Nov 1 '12 at 20:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.