HOWTO · Bash
How to Solve Export Not a Valid Identifier Error in Bash
Learn why Bash reports that an exported name is not a valid identifier, how to fix spaces around assignments and invalid names, and how to check a scalar in a child command.
On this page
The Bash export command reports “not a valid identifier” when an argument that should name a variable does not follow Bash’s identifier rules. Two frequent causes are a name that starts with a digit and spaces around the assignment operator. Use a name beginning with a letter or underscore, write assignments without spaces around =, and quote the value when it contains spaces. Exported scalar variables are available to child commands; Bash arrays are not exported as arrays.
What the “not a valid identifier” Error Means
A Bash variable identifier starts with an ASCII letter or underscore, followed by zero or more letters, digits, or underscores. Names are case-sensitive, so APP_MODE and app_mode are different variables. A digit may appear after the first character, but not at the beginning. Spaces, hyphens, periods, and other punctuation are not permitted in an identifier.
export accepts either a variable name or an assignment in the form name=value. When the assignment is split by spaces, Bash receives separate arguments instead of one assignment. It can then treat = as an argument to export and report it as an invalid identifier. The exact diagnostic wording can vary with the argument that Bash rejects.
Check the Export Syntax and Identifier
Write the assignment as one shell word, with no spaces on either side of =:
export APP_MODE=production
By contrast, export APP_MODE = production is parsed as three arguments: APP_MODE, =, and production. Remove the spaces around the operator. If the malformed command appears in a startup file such as ~/.bashrc or ~/.bash_profile, edit that line there; the file itself is not the cause unless it contains a command with the problem. For a quick search, inspect those files for the variable name or the export line, then start a new shell or source the edited file to apply it.
Fix Invalid Variable Names
A name such as 1st_variable starts with a digit, so it cannot be used as a Bash variable. Rename it to a valid identifier, for example first_variable. This standalone example captures the expected failure so the shell script can continue, then exports the corrected name and checks it in a child Bash process:
if export 1st_variable=hello 2>/dev/null; then
printf '%s\n' 'Unexpectedly accepted the name'
else
printf '%s\n' 'The name beginning with a digit was rejected'
fi
export first_variable=hello
bash -c 'printf "%s\n" "$first_variable"'
The example is provided for the reader to run; no execution result is asserted here. The corrected identifier follows the naming rule, and the child command receives its exported value through the environment. If you need to retain the original label for display or data lookup, store that label as a value rather than using it as a variable name.
Handle Values with Spaces
A variable name cannot contain spaces, even when you put the name in quotes. Quoting only changes how the shell groups and interprets words; it does not change identifier rules. Use an underscore in the name and quote the right-hand-side value when it contains spaces, such as export GREETING="Hello World". The quotes preserve the value as one assignment, while GREETING remains a valid identifier.
When expanding a value, quote the expansion too: printf '%s\n' "$GREETING". This prevents word splitting and pathname expansion if the value contains spaces or wildcard characters. Avoid export $name=value when name is data: expanding arbitrary text into shell syntax is fragile, and the resulting assignment may not mean what you intend.
Use Arrays Without Trying to Export Them
Bash arrays can hold several related values within the current Bash process, but the process environment represents variables as strings. Exporting an array does not transmit its indexed or associative structure to a child process. Do not rely on export my_array to make an array available as an array elsewhere.
If a child command needs one value, pass or export a scalar variable. If it needs several values, choose an explicit representation that the child understands, such as separate scalar environment variables or a documented delimiter format, and parse that format in the child. A delimiter format is suitable only when the values cannot contain that delimiter or when you define escaping rules. Keep an array when it is only needed inside the current Bash script.
Verify the Exported Variable
Printing a variable in the current shell confirms that it has a value there, but does not by itself prove that it was exported. Check from a child process instead. Run this example:
export APP_MODE=production
bash -c 'printf "%s\n" "$APP_MODE"'
The child command prints the inherited value when the variable is exported. If it prints a blank line, check the spelling, assignment syntax, and whether the export ran in the parent shell before starting the child.
For commands in startup files, make the correction in the file that contains the malformed assignment, then open a new interactive shell or source that file. Verify the result from a child command started in that same shell. This separates a naming or parsing problem from a variable that was simply set in a different shell process.