変数名に「-」を含めることはできますか? [コピー]

変数名に「-」を含めることはできますか? [コピー]
$ my-num=1
my-num=1: command not found

$ mynum=1

最初の課題が失敗した理由を知りたいです。

bashはなぜそれがコマンドだと思いますか?

-Bashに特殊文字がありますか?

ありがとうございます。

ベストアンサー1

POSIX 標準には以下が指定されています。

3.229 名前

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.

Shell Syntax RulesのPOSIX標準セクションには、次のように記載されています。

2.10.2 シェル構文規則

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).

変数名に無効な文字が含まれているため、割り当てを試みずに単純なコマンドとして扱います。

おすすめ記事