What are the rules for valid identifiers (e.g. functions, vars, etc) in Bash?

后端 未结 5 614
终归单人心
终归单人心 2020-12-03 17:02

What are the syntax rules for identifiers, especially function and variable names, in Bash?

I wrote a Bash script and tested it on various versions of Bash on Ubuntu

5条回答
  •  长情又很酷
    2020-12-03 17:19

    From the manual:

       Shell Function Definitions
           ...
           name () compound-command [redirection]
           function name [()] compound-command [redirection]
    

    name is defined elsewhere:

           name   A  word  consisting  only  of alphanumeric characters and under‐
                  scores, and beginning with an alphabetic character or an  under‐
                  score.  Also referred to as an identifier.
    

    So hyphens are not valid. And yet, on my system, they do work...

    $ bash --version
    GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
    

提交回复
热议问题