7

What are the acceptable characters in a bash variable?

Can someone point me to the place in the bash man page that describes the acceptable name for a bash variable?

I looked and came up with a lot of info (see question edits), but not a clear answer.

4
  • May be relevant.
    – Sparhawk
    Mar 7, 2018 at 23:07
  • 2
    So... what is the question?
    – DopeGhoti
    Mar 7, 2018 at 23:10
  • @DopeGhoti edited for clarity
    – 111---
    Mar 8, 2018 at 15:48
  • 2
    I'm voting to close this question as off-topic because while it's a great write-up for the rules for variable names, there does not appear to be an actual question in this question.
    – DopeGhoti
    Mar 8, 2018 at 15:59

1 Answer 1

15

You can assign values to variables (with var=value). There's a fine distinction between a parameter and a variable:

A parameter is an entity that stores values. It can be a name, a number, or one of the special characters listed below. A variable is a parameter denoted by a name.

and a name is defined as:

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.

(That's the definition in Bash's manual, POSIX probably has something similar.)

So, anything that matches the regex [a-zA-Z_][a-zA-Z_0-9]* (assuming ASCII character ranges) is a valid name for a variable. Unicode characters and punctuation don't work.

That's pretty much the set of characters supported in variable names in most programming languages, though some do support Unicode, too. Words beginning with digits are taken as numbers (note that stuff like 0xabcd and 123e3 are often valid numbers), and the dash is usually taken as the minus operator, so a-b is a simple arithmetic operation of two variables, not one variable. And so on.

The parameters that aren't of that format are the numbered positional parameters (script/function arguments, assignable with set) and the special parameters. Note that _ (a single underscore) is one of the latter, and while you can assign to it without an error, it's not much use since it resets immediately.

These are fine:

$ a123=foo __=bar
$ echo $a123 $__
foo bar

but:

$ _=foo echo bar >/dev/null 
$ echo $_
bar

As for the uppercase ones, there's a list of the variables that are special to Bash or other Bourne-ish shells (ignore the readline variables, they're not related here). They all do seem to start with an uppercase letter, except that there's histchars that's all lowercase(!). Luckily, it only matters if history expansion is enabled.

3
  • You can have spaces either side of the egual sign in var=value in certain cases. Example: (( var = 6 )); echo $var;
    – fpmurphy
    Mar 8, 2018 at 1:48
  • You can have unicode characters as variable name, try to set your locale
    – cuonglm
    Mar 8, 2018 at 2:13
  • @cuonglm, Doesn't work in Bash (Debian's 4.4.12(1)-release). LC_ALL=fi_FI.UTF-8 bash -c 'ä=123' gives bash: ä=123: command not found. ksh and zsh seem to accept that, though.
    – ilkkachu
    Mar 8, 2018 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .