$ 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.
$ 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.
The POSIX standard says the following:
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:
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.
$PATH
, and didn't find it. So it said "command not found".
– AlexP
May 26 '18 at 17:02
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