Sandra Schlichting
Sandra Schlichting

Reputation: 26006

Explanation of how ${test##*.} and ${test%%.*} works?

Is there a good way to explain how the below works?

~$ echo $test
en.to.tre
~$ echo ${test}
en.to.tre
~$ echo ${test%.*}
en.to
~$ echo ${test%%.*}
en
~$ echo ${test#*.}
to.tre
~$ echo ${test##*.}
tre

In particular I don't understand why . and * have to be swapped when removing/keeping from left/right.

Upvotes: 2

Views: 128

Answers (1)

Uwe
Uwe

Reputation: 728

.* means "substring starting with ."; *. means "substring ending with .". In the third and the fourth line, you remove the shortest/longest substring starting with . from the end; in the fifth and sixth line, you remove the shortest/longest substring ending with . from the beginning.

The strings after #, %, etc., are interpreted as globbing patterns (like filenames), not as regular expressions, so . stands for itself.

Upvotes: 4

Related Questions