0

I've read through multiple postings and can't seem to export a variable with a special character in Centos 7.5 (I believe the special character is the problem, please correct me if it's something else). The command export TEST-ME=blah returns -bash: export: TEST-ME=blah: not a valid identifier. I've tried escaping with a backslash and multiple variants of single and double-quotes with the same result. What am I doing wrong?

risail
  • 103

1 Answers1

5

While environment variables can contain any character or non-character to the exception of NUL and =, the export special builtin of POSIX shells exports a shell variable to the environment.

In most Bourne like shells, shell variables have to start with an alphabetical character (often limited to those of the POSIX portable character set) or underscore followed by alphanumerical characters (or underscore). - is generally not classified as an alphanumerical character.

To run a command with TEST-ME=blah passed in its environment, you can however do:

env TEST-ME=blah a-command

rc and its derivatives can have any character in its shell variables and shell variables are automatically exported to the environment of commands executed by the shell.

However, with the variant by Byron Rakitzis (as opposed to the original one from Plan9 / Unix V10 now opensourced), characters other than alnums-and-underscore from the portable charset are encoded as _xx where xx is the hexadecimal representation of the values of the bytes that constitute those characters.

So with rc, you can do:

TEST-ME = blah; my-command

But depending on the rc implementation, my-command will either get TEST-ME=blah or TEST__2dME=blah in the environment it receives from the shell.

In any case, it's a bad idea to use environment variable names that contain characters other than the alnums from the portable character set (or that start with digits or that are empty) as several applications including some shells (such as mksh) remove them upon import.

For instance,

env TEST-ME=blah mksh -c 'printenv TEST-ME'

returns nothing.

With other shells, you can always re-execute the shell with TEST-ME=blah passed to its environment. While shells won't map those environment variables to shell variables, some of them will still pass them along to the commands they execute. So:

exec env TEST-ME=blah /proc/self/exe

for instance would be a way to sort-of add TEST-ME=blah to the environment.

  • got flagged as a duplicate, for a nonduplicate q. classic stack. Thanks for answering @Stéphane Chazelas – risail Jan 12 '21 at 17:39
  • @risail Please, what part or aspect of the question makes it not a duplicate? I'm more than happy to remove the duplication if it is warranted. Also note that I may be a user that is an elected moderator, but I am most definitely not "StackExchange" (if that is what you mean by "stack"). As I said, I'm more than happy to reopen the question even though nobody else seems to have voted for this to happen, if it is warranted. – Kusalananda Jan 12 '21 at 18:53