1
$ my-num=1
my-num=1: command not found

$ mynum=1

I was wondering why the first assignment fails?

Why does bash think that it is a command?

Is - some special character in bash?

Thanks.

Tim
  • 101,790
  • @steeldriver: that question is also a duplicate and I don't think either answers this question. – jesse_b May 26 '18 at 16:29
  • I think it does, @Jesse_b: “A valid variable name start with a letter capitalized or uncapitalized. The following characters can be letters, digits or an underscore (_).” – Jeff Schaller May 26 '18 at 17:31
  • @JeffSchaller: True. This question is actually 3 questions though, which I suppose is also not okay. – jesse_b May 26 '18 at 17:33

1 Answers1

8

The POSIX standard says the following:

3.229 Name

3.229 Name
In the shell command language, a word consisting solely of underscores, digits, and 
alphabetics from the portable character set. The first character of a name is not a digit.

In the POSIX standard section for Shell Grammar Rules it states:

2.10.2 Shell Grammar Rules

7. [Assignment preceding command name]
  a. [When the first word]
    If the TOKEN does not contain the character ’=’, rule 1 is applied. 
    Otherwise, 7b shall be applied.
  b. [Not the first word]
    If the TOKEN contains the equal sign character:
      — If it begins with ’=’, the token WORD shall be returned.
      — If all the characters preceding ’=’ form a valid name (see XBD Section 3.229, |
        on page 65), the token ASSIGNMENT_WORD shall be returned. (Quoted
        characters cannot participate in forming a valid name.)
      — Otherwise, it is unspecified whether it is ASSIGNMENT_WORD or WORD
        that is returned.
  Assignment to the NAME shall occur as specified in Section 2.9.1 (on page 2263).

Since the variable name contains invalid characters it does not attempt assignment but instead treats it as a simple command.

jesse_b
  • 37,005
  • Thanks. Why does bash think that it is a command? – Tim May 26 '18 at 16:48
  • 3
    @Tim: What else could it be? It's the first word in a command line; so it's either an alias (and it isn't, the shell checked), or a function (and it isn't, the shell checked), or an internal command (and it isn't, the shell checked). So it must be the name of an executable file. The shell looked for it in the directories in $PATH, and didn't find it. So it said "command not found". – AlexP May 26 '18 at 17:02
  • @Tim: I've updated my answer. – jesse_b May 26 '18 at 17:03
  • Thanks. In my-num, - doesn't appear after $ or within ${} or after whitespace. So does bash really think of - in the variable name as any of the three cases? – Tim May 26 '18 at 17:05
  • @Tim: Updated again. POSIX says the only valid characters are underscores, digits, and letters. – jesse_b May 26 '18 at 17:09
  • 1
    In theory, Bash could have expanded the set of characters that are valid in variable names, so we'll need to check Bash's manual. As it happens, it contains the same definition, in different words. – ilkkachu May 26 '18 at 20:23