ru Русский

Reticularium

NETWORKS PLACE

It’s not easy to remember, so here’s the reference.

% means “right”

# means “left”

(look at the keyboard if you forgot, these signs relative positions can remind their meaning)

single sign means “the first”, double sign means “the last”.

So, a single % means “the first starting from right”, a double ## means “the last starting from left”.

.* means “everything on the right from the dot”. Instead of the dot any other symbol can be used. Nothing can be used instead of the asterisk symbol it seems, at least I couldn’t find anything.

Similarly, *. means “everything on the left form the dot”

Let’s assign var=aaa.bbb.ccc

There are 8 possible combinations, but not all of them work:

  % %% # ##
  .*  ${var%.*} => aaa.bbb ${var%%.*} => aaa ${var#.*} => aaa.bbb.ccc (doesn’t work) ${var##.*} => aaa.bbb.ccc (doesn’t work)
  *.  ${var%*.} => aaa.bbb.ccc (doesn’t work) ${var%%*.} => aaa.bbb.ccc (doesn’t work) ${var#*.} => bbb.ccc ${var##*.} => ccc

Here’s the example.

The code below cuts off sub-domains and returns domain name (2nd level domain), e.g. abc.def.example.com -> example.com


domain2l=`a12=${domain%.*} ; a3=${domain##*.} ; a2=${a12##*.} ; echo "$a2.$a3"`

where domain is any domain name with any number of sub-domains, and domain2l is a resulting 2nd level domain.

Comment it: