Break statement of bash cannot be defined in a function from bash-4.4

Solution Verified - Updated -

Issue

  • There is test shell using break statement in a function. It could be executed in RHEL7 normally but failed in RHEL8 and RHEL9.

    # cat test.sh
    #!/bin/bash
    
    while true;do
      printf "job start\n"
    
      function testBreak(){
          printf "function start\n"
          break
      }
    
      testBreak
    done
    
  • Test result in RHEL7.

    # rpm -q bash
    bash-4.2.46-35.el7_9.x86_64
    # ./test.sh
    job start
    function start
    
  • Test result in RHEL8 and RHEL9.

    # rpm -q bash
    bash-4.4.20-5.el8.x86_64
    # ./test.sh
    job start
    function start
    ./test.sh: line 8: break: only meaningful in a 'for', 'while', or 'until' loop
    job start
    function start
    ./test.sh: line 8: break: only meaningful in a 'for', 'while', or 'until' loop
    ...
    

Environment

  • Red Hat Enterprise Linux 8
  • Red Hat Enterprise Linux 9
  • Red Hat Enterprise Linux 10
  • bash-4.4 and later

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content