How to replace any one of many possible matches in bash

I want to reduce this down to one line:

item=${_menu_sel_value/M--/}
item=${item/-M-/}
item=${item/D--/}
item=${item/-D-/}
item=${item/??-/}

Here is a test sample. User chooses one of the following from a shell menu and their choice is passed into bash script code above (so, only need to match ONE of the possibilities):

 -D-branch.sh
 M--cfg/aliases.cfg
 -M-functions/0300.menu.sh
 M--git_aliases.sh
 ??-add.sh

Is it possible?

I tried something like:

item=${_menu_sel_value/^[M\-]/}

But it didn't work.

Answers


I think this is what you are looking for.

shopt -s extglob
item=${_menu_sel_value/@(M--|-M-|D--|-D-|\?\?-)/}

The pattern @(a|b|c) matches any one of the patterns a, b, or c. Note the need to escape the ?, as it is a wildcard that matches any single character in a pattern.


Need Your Help

Returning an Updated Row in Integration Test C#

c# sql sql-server stored-procedures

In my code I originally have a test method that disable a row by calling a stored procedure. My stored procedure updates the disable column if the value changed from the previous value in the datab...

Executing long command with groovy on linux shell?

linux bash shell groovy sh

How can I execute a long command with groovy?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.