Bash: why does comparison priority matter with wildcards?

Does it work ==like an operator INwhen a test string contains wildcards? This is very magical ...

example1:

string='My long string';
if [[ $string == *"My long"* ]]; then   echo "It there!"; fi

conclusion:

It There

example2:

if [[ *"My long"* == $string ]]; then   echo "It there!"; fi

There is no conclusion.

+4
source share
2 answers

The way it [[ ]]was designed to work. This is in the manual:

When the operators '== and' are used! =, the line to the right of the operator is considered a template and matched in accordance with the rules described below in Match Matching.

+4
source

The Glob template only works on the right side of the comparison, so

if [[ *"My long"* == $string ]]; then   echo "It there!"; fi

will fail.

Even this will not work:

[[ *"My long"* == "My long" ]] && echo "It there!"

But this will work:

[[ "My long" == *"My long"* ]] && echo "It there!"
It there!

, :

string='My long string';
if [[ $string == *"My long"* ]]; then   echo "It there!"; fi

- glob RHS, *

- "My long", "My long", .

+2

Source: https://habr.com/ru/post/1568213/


All Articles