Bash substrings – just can’t figure it out…

Johnny Rosenberg gurus.knugum at gmail.com
Tue Jan 31 18:07:20 UTC 2012


I have read some about those built in substring tools built into Bash,
but still I don't understand them enough to use them.

Here's what I found at http://tldp.org/LDP/abs/html/string-manipulation.html:
”${string#substring}

Deletes shortest match of $substring from front of $string.
${string##substring}

Deletes longest match of $substring from front of $string.”

I have an example here:
File="03. Rock Nuts.flac"
echo "${File##N*s}"

The result should be ”03. Rock .flac”, shouldn't it? Obviously not,
because the result is ”03. Rock Nuts.flac”, which is exactly the
original string.
So what am I missing?

What I originally really wanted to achieve was something quite
different: The opposite of the above; cutting out everything BUT
something. In this case I wanted to cut everything between ”. ” and
”.”, so that only ”Rock Nuts” is left in the example above. I know I
can do that very easily with awk, but how can I do it with the Bash
built in stuff?



Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ




More information about the ubuntu-users mailing list