'How to use commands inside Shell function while having quotes and semicolon

I have borrowed below code from the google and modified it a little but somehow it not giving the expected output.

Shell function:

remote_collect() {
    target_host=$1
    {
        read -r rhelInfo
        read -r perlInfo
    } < <(
          ssh -i my_connect "root@${target_host}" \
              -o StrictHostKeyChecking=no -o PasswordAuthentication=no \
              /bin/bash <<-EOF
              cat /etc/redhat-release | awk 'END{print \$7}'
              #rpm -qa --last | awk '/kernel-[0-9]/'|head -1
              rpm -qa --last | awk '/kernel-[0-9]/{ first=$1; $1=""; print $0 }'
EOF
          ) 2>/dev/null

Where is Problem:

In the above function the command rpm -qa --last | awk '/kernel-[0-9]/{ first=$1; $1=""; print $0 }' does not produce anything while running as an script however if i use rpm -qa --last | awk '/kernel-[0-9]/'|head -1 then this works perfectly, So, where i am missing something like escaping something?

Please help to make me understand or resolve this.



Solution 1:[1]

You should escape all the $ sequence in Order to work, and it should work.

Please try below.

rpm -qa --last | awk '/kernel-[0-9]/{ first=\$1; \$1=""; print \$0 }'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karn Kumar