Search code examples
linuxbashpattern-matchingglob

Why can't I use wildcard patterns in variable assignments?


In bash this works:

var1=abc

But this gives an error:

var*1=abc
var*1=abc: command not found

Why so? Why is the expression treated as a command?


Solution

  • From http://www.gnu.org/software/bash/manual/bashref.html

    name

    A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.

    The value on the left hand side is not an identifier. It is an expression. Therefore that command (to evaluate the entire expression) is invalid.

    In other words, you can't have asterisks in names, and you can't generate a variable name by doing some math (multiplying).