Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat 
-Werror=format-security -Wall
uname output: Linux x2 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) 
x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: 4
Release Status: release

Description:

Parameters expanded to null by pattern removal or pattern substitution in the
patterns of case clauses produced unexpected results in some cases.

The version of bash in my system is 5.1, but the same results are produced in
bash 5.2.37 I built from source.

Repeat-By:

In the following example, I expected no output, but in fact it output "not
matched".

    x=abc
    case def in
        "${x#abc}"def) ;;
        "${x##abc}"def) ;;
        "${x%abc}"def) ;;
        "${x%%abc}"def) ;;
        "${x/abc/}"def) ;;
        "${x//abc/}"def) ;;
        "${x/#abc/}"def) ;;
        "${x/%abc/}"def) ;;
        *) echo 'not matched'
    esac
    # => not matched

For comparison, the following cases are similar to the above but worked as
expected.

    # case 1: assign the result of the expansion to another variable
    x=abc
    y=${x#abc}
    case def in
        "$y"def) echo matched
    esac
    # => matched

    # case 2: the parameter expansion is not quoted
    x=abc
    case def in
        ${x#abc}def) echo matched
    esac
    # => matched

    # case 3: the double-quoted string is not null after expansion
    x=abc
    case def in
        "${x#abc}def") echo matched
    esac
    # => matched

    # case 4: the target word of pattern matching is null
    x=abc
    case '' in
        "${x#abc}") echo matched
    esac
    # => matched

    # case 5: variable x is null (only for #, ##, %, and %%)
    x=
    case def in
        "${x#abc}"def) echo matched
    esac
    # => matched

        # note: Null variable x with pattern substitution does not work as
        #       expected.
        x=
        case def in
            "${x/abc/}"def) ;;
            "${x//abc/}"def) ;;
            "${x/#abc/}"def) ;;
            "${x/%abc/}"def) ;;
            *) echo 'not matched'
        esac
        # => not matched


Reply via email to