Skip to content

First case pattern that starts with a hyphen is mis-tokenized as extglob_pattern #336

Open

Description

@Doodler8888

In a case statement, the first case pattern is tokenized as extglob_pattern if it starts with a hyphen. All subsequent patterns are parsed correctly.

Reproduction code:

#!/bin/bash

case "$1" in
  -first) echo 1;;
  second) echo 2;;
  third) echo 3;;
  *) echo wildcard;;
esac

Parse tree:

(program (comment)
 (case_statement case
  value: 
   (string "
	(simple_expansion $ (variable_name))
	")
  in
  (case_item value: (extglob_pattern) )
   (command
	name: (command_name (word))
	argument: (number))
   termination: ;;)
  (case_item value: (word) )
   (command
	name: (command_name (word))
	argument: (number))
   termination: ;;)
  (case_item value: (word) )
   (command
	name: (command_name (word))
	argument: (number))
   termination: ;;)
  (case_item value: (extglob_pattern) )
   (command
	name: (command_name (word))
	argument: (word))
   ;;)
  esac))

If i remove the hyphen:

#!/bin/bash

case "$1" in
  first) echo 1;;
  second) echo 2;;
  third) echo 3;;
  *) echo wildcard;;
esac

Then it becomes:

(program (comment)
 (case_statement case
  value: 
   (string "
	(simple_expansion $ (variable_name))
	")
  in
  (case_item value: (word) )
   (command
	name: (command_name (word))
	argument: (number))
   termination: ;;)
  (case_item value: (word) )
   (command
	name: (command_name (word))
	argument: (number))
   termination: ;;)
  (case_item value: (word) )
   (command
	name: (command_name (word))
	argument: (number))
   termination: ;;)
  (case_item value: (extglob_pattern) )
   (command
	name: (command_name (word))
	argument: (word))
   ;;)
  esac))

Activity

  1. Doodler8888 commented on May 1, 2026

    @Doodler8888
    Author

    also adding underscore inside any case patterns changes it from word to extglob_pattern

    #!/bin/bash
    
    case "$1" in
      first) echo 1;;
      s_econd) echo 2;;
      third) echo 3;;
      *) echo wildcard;;
    esac
    
    (program ; [0, 0] - [8, 0]
      (comment) ; [0, 0] - [0, 11]
      (case_statement ; [2, 0] - [7, 4]
        value: (string ; [2, 5] - [2, 9]
          (simple_expansion ; [2, 6] - [2, 8]
            (variable_name))) ; [2, 7] - [2, 8]
        (case_item ; [3, 2] - [3, 17]
          value: (word) ; [3, 2] - [3, 7]
          (command ; [3, 9] - [3, 15]
            name: (command_name ; [3, 9] - [3, 13]
              (word)) ; [3, 9] - [3, 13]
            argument: (number))) ; [3, 14] - [3, 15]
        (case_item ; [4, 2] - [4, 19]
          value: (extglob_pattern) ; [4, 2] - [4, 9]
          (command ; [4, 11] - [4, 17]
            name: (command_name ; [4, 11] - [4, 15]
              (word)) ; [4, 11] - [4, 15]
            argument: (number))) ; [4, 16] - [4, 17]
        (case_item ; [5, 2] - [5, 17]
          value: (word) ; [5, 2] - [5, 7]
          (command ; [5, 9] - [5, 15]
            name: (command_name ; [5, 9] - [5, 13]
              (word)) ; [5, 9] - [5, 13]
            argument: (number))) ; [5, 14] - [5, 15]
        (case_item ; [6, 2] - [6, 20]
          value: (extglob_pattern) ; [6, 2] - [6, 3]
          (command ; [6, 5] - [6, 18]
            name: (command_name ; [6, 5] - [6, 9]
              (word)) ; [6, 5] - [6, 9]
            argument: (word))))) ; [6, 10] - [6, 18]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions