Ankur Agarwal
Ankur Agarwal

Reputation: 24768

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?

Upvotes: 0

Views: 178

Answers (3)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

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

Upvotes: 1

geekosaur
geekosaur

Reputation: 61389

POSIX defines shell variable names to consist of alphanumerics and underscore, and not starting with a digit. (There are special variables which violate this; they are always single characters, and they cannot collide with user defined variables.)

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

Upvotes: 1

Invalid character (asterisk) for a variable identificator.

Upvotes: 1

Related Questions