regex – Catch token word in bash ‘heredoc’

The beginning of a ‘heredoc’ string in bash usually looks like

cat <<EOF or cat << EOF

i.e. there may or may not be a space between the two less-than characters and the token word “EOF”. I would like to catch the token word, so I try the following

$ pcretest
PCRE version 8.45 2021-06-15

  re> "^\s*cat.*[^<]<{2}[^<](.*)"
data> cat << EOF
 0: cat << EOF
 1: EOF
data> cat <<EOF
 0: cat <<EOF
 1: OF

As you can see in the string where there is no space between the << and EOF, I only catch “OF” and not “EOF”. The expression must match exactly two less-than signs and fail if there are three or more. But why does it gobble up the ‘E’ so that only “OF” is returned?

Read more here: Source link