2

Say we have a file called "Hello World: The easiest program" and its name is saved in a variable called "title".

I want to match this with glob pattern matching, but remove the colon. In other words, I want it to match "Hello World The easiest program"

I found out you can do this by using the following:

${/:/|${title}|}

It works, but I don't understand why.

I know that "|" is OR, so to me it looks like it matches "${/:/}" or "${${title}}" or "${}", none of which look like they're removing the colon.

So my question is: How/why does the glob pattern matching expression above remove colons from a title?

Edit: I should mention the shell I'm using is the Steam ROM Manager. And it seems I was mistaken and it's not a glob expression, but a Steam ROM Parser function variable that optionally uses glob syntax (in place of the default regex syntax).Thanks Steeldiver and ilkkachu!

5
  • 3
    what shell are you using? It seems that ${/:/|${title}|} doesn't work for my current shells (bash and zsh) Nov 5, 2022 at 21:45
  • 2
    Could you give a full example of this?
    – Hack Saw
    Nov 5, 2022 at 22:11
  • I am using the Steam ROM Manager on Steam Deck. I didn't understand that the thing between the forward slashes will be replaced. Nov 6, 2022 at 9:38
  • Parser Type in Steam ROM Manager was set to Glob. With Hack Saws help, I was now able to use this knowledge to remove brackets, parentheses, their contents and any whitespaces before or after using: ${/(\s?\[.*?\]\s?)/|${/(\s?\(.*?\)\s?)/|${/:/|${title}|}|} It first takes the title, replaces colons with nothing, then takes the result and replaces the first thing that matches \s?\(.*?\)\s? and replaces it with nothing and then takes the result again and replaces the first thing that matches \s?\[.*?\]\s? and replaces it with nothing Nov 6, 2022 at 9:46
  • 3
    The Steam ROM Manager seems hardly a common Unix-y shell, so in the least you really should edit your question to include that context.
    – ilkkachu
    Nov 6, 2022 at 17:32

2 Answers 2

3

The expression in your question is not a glob expression, it's apparently a Steam ROM Parser function variable that optionally uses glob syntax (in place of the default regex syntax) for its pattern matching sequence.

The expression is of the form

${/pattern/|input|}

with pattern equal to : and input equal to variable expansion ${title}. In this context | is to be read as a kind of ternary conditional

|on match|no match(optional)

So ${/:/|${title}|} means to match occurences of : in ${title} and replace them with nothing.

Note that since : is not special in either shell glob or regular expression syntax, it shouldn't actually matter whether you set the Parser Type to glob or otherwise.

0

In bash, assuming you have title="Hello World: The easiest program", if you use Parameter Expansion like this:

echo ${title/:/}

It will print out

Hello World The easiest program

${title/:/} says "take the parameter title, but the first time there is a ':', sub in a ''." (That is, nothing)

If you use an extra /, like this: ${title//:/} it will substitute all of them.

title="Hello World: The easiest:::::: program"
echo ${title//:/}
Hello World The easiest program

With an obvious substitution:

title="Hello World: The easiest:::::: program"
echo ${title//:/*}
Hello World* The easiest****** program

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .