Sign up ×

I am not able to use - in variables in shell. Is there a way to be able to use it, because I have one script which depends on such named variables:

$export a-b=c
-bash: export: `a-b=c': not a valid identifier

$export a_b=c

First throws the given error and second works fine.

share|improve this question

9 Answers 9

up vote 16 down vote accepted

I've never met a Bourne-style shell that allowed - in a variable name. Only ASCII letters (of either case), _ and digits are supported, and the first character must not be a digit.

If you have a program that requires an environment variable that doesn't match the shell restrictions, launch it with the env program.

env 'strange-name=some value' myprogram
share|improve this answer

That's not possible in Bash.

From the Definitions section in the manual page of bash:

name A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier.

From the Parameters section in the manual page of bash:

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

share|improve this answer
+1 for demonstrating the usefulness of man pages once more –  ktf Oct 31 '11 at 11:48
Old post I know but, I want to point out that to newcomers man pages can be quite cryptic. I still have times when I need hunt down better explanations/examples. Anyone would be lying if they said it never happened to them. –  TCZ8 Dec 17 '14 at 15:08

The dash (-) character is a break character and not allowed as part of variable names. There are ways to hack this with quoted variables, but the parsing of it is really problematic. There are also other characters with special meanings in the context of variable names in bash, notably braces, parenthesis, operator characters and quotes. (e.g. {}()=+-&'" and more)

I would suggest that practically you need to find another paradigm on which to build your script. You might have a hang over idea from other languages about "variable variable names". This is generally not a good idea in shell scripts.

If you edit this or ask a new question with details of your context and what you are trying to accomplish we might be able to suggest a good way to script it.

share|improve this answer

Most shells only support a-z, A-Z, 0-9 and _ for variablenames. Read second item in this page.

share|improve this answer

I believe that only letters, numbers, and underscore are allowed for bash variables. This is the case in many programming languages (javascript being an exception).

I recommend not having your script depend on those types of variable names.

In fact, you should try to program in such a way that you can replace variable names with other names and it doesn't make a difference. In general, variable names should describe what the variable contains. This makes it much easier to debug; if not for you, then for the next developer that is trying to figure out the code.

share|improve this answer

The Bash manual defines a "name" as:

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

So you can't use a hyphen in a name.

share|improve this answer

It sounds like there's a conceptual problem here rather than a programming one. It would be useful for you to elaborate on what you actually need the script to do.

A script is essentially an algorithm (or heuristic) for solving a problem. It is a tool that produces a solution, not the solution itself. Because of this and because the scope of most variables in a script is restricted to within the script itself and possibly to other scripts it calls, it should make no functional difference what your variables are called. The only (but important) significance of variable names is their contribution to making the script readable and understandable to a human being. The computer (shell) doesn't care.

Anything you need to look a specific way will probably be output of the script and, with a little cleverness, you can have your script output almost any character string you can imagine in almost any language - even those that used to be used to make a character-based terminal look a little like a gui with colored text and backgrounds, etc.. There are even simple tools that allow a script to use gui dialog boxes and other graphical elements.

share|improve this answer

If your script depends on having variable names have hyphens, that's a programming error. If it is convenient for you because of the tools that you regularly use to have the variable names contain a hyphen, you may have to learn more and different tools.

Have you tried using tr to convert the hyphens into underscores?

unhyphenated_name=$(echo $hyphenated_name | tr '-' '_')
declare -x $unhyphenated_name="some value"

Bash does allow '-' to appear in function names. I do this all the time. For example:

function foo-bar() {
   echo "$@"
share|improve this answer

You can access a hyphenated variable using an indirect reference.

$ env 'my-hyphenated-variable=hello' /bin/bash
$ name='my-hyphenated-variable'
$ echo ${!name}
share|improve this answer
I had never heard of this functionality until reading this comment.Provided a much easier solution for me than the accepted answer. –  DrStrangepork Aug 26 at 15:03

Your Answer


