How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters such as: ^[^#]*=, line%%=*, line#*= . how to understand the usage of these special character here. Thanks.

read_cfg()
{��
  while read line; do��
     if [[ "$line" =~ ^[^#]*= ]]; then��
        l_parameter=`echo ${line%%=*} | tr -d ' '` l_value=`echo ${line#*=} | tr -d ' '`��
��
        case "$l_parameter" in ��
           "EMAIL_LIST" ) ��      #email list provided in command line overwrites the one in configuration file��
               if [ -z "$g_bkp_email" ] ; then��
                  g_bkp_email=$l_value��
               fi��
               ;;��

Not sure I'm an expert nor a guru, but this is what I'd do: refer to the respective man pages.
man bash :

Of course you need to learn the difference between shell's pattern matching and regex matching.

man regex :

Admittedly the ambiguous use of the caret is something you need to accustom to.

2 Likes

^[^#]*= is an extended regular expression and it means 0 or more non-# characters starting at the beginning of the line followed by an equal sign..

${line%%=*} is parameter expansion and it returns the characters to the left of the first = sign
${line#*=} is parameter expansion and it returns the characters to the right of the leftmost = sign

3 Likes

RudiC, Scrutinizer:

Thanks a lot to both of you. The explanation is clear and make me better understanding on these special characters. I will learn more.

Moderator comments were removed during original forum migration.

There's many great tools out there. One I recommend on a regular (bad pun) basis is https://regexr.com/. It includes a "deduced" explanation if you have an expression you find and paste in.

1 Like