Why with and without * make difference in Bash

I'm trying to figure out parameter substitution, especially about ${var#Pattern} and ${var##Pattern}.

According to the doc:

${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end of $var.

${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.

Now when I run the following, I expected that the third and the fourth will be the same but they are not.

Why is it?

How does the * effect in this case comparing to the first and second example?

#!/usr/bin/env bash

myString="Hello Hello World!"

echo "${myString#*$Hel}"     # lo Hello World!
echo "${myString#$Hel}"      # lo Hello World!
echo "${myString#*$ll}" # o Hello World!
echo "${myString#$ll}"  # Hello Hello World!


lo Hello World!
lo Hello World!

o Hello World!
Hello Hello World!

Read more here: https://stackoverflow.com/questions/66332521/why-with-and-without-make-difference-in-bash

Content Attribution

This content was originally published by shin at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: