LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Is it possible to set an environment variable name containing a period in BASH? (https://www.linuxquestions.org/questions/linux-general-1/is-it-possible-to-set-an-environment-variable-name-containing-a-period-in-bash-724256/)

dnwake 05-06-2009 06:36 PM

Is it possible to set an environment variable name containing a period in BASH?
 
I want to export several environment variables to a subprocess. As a result of circumstances outside my control, the names of the variables contain periods. In other words, I want to do something like this

> export foo.bar=123
bash: export: `foo.bar=123': not a valid identifier

I've tried using quotes and backslashes. Is it just impossible to do this?

Thanks

i92guboj 05-06-2009 07:03 PM

Quote:

Originally Posted by dnwake (Post 3532684)
I want to export several environment variables to a subprocess. As a result of circumstances outside my control, the names of the variables contain periods. In other words, I want to do something like this

> export foo.bar=123
bash: export: `foo.bar=123': not a valid identifier

I've tried using quotes and backslashes. Is it just impossible to do this?

Thanks

Yes. It's impossible.

Quote:

Originally Posted by bash man page
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.

I am curious as to why would it matter how an external process names the variables... You can name them internally as you wish. In any case you will be passing the variables to the bash script either as positional arguments or through a file, so... can you elaborate on why do you think you need to use a dot in the identifiers?

dnwake 05-06-2009 07:42 PM

I want to pass the variables to an external process launched from BASH. Is there any way to do this, other than using export?

Cheers.



Quote:

Originally Posted by i92guboj (Post 3532695)
Yes. It's impossible.



I am curious as to why would it matter how an external process names the variables... You can name them internally as you wish. In any case you will be passing the variables to the bash script either as positional arguments or through a file, so... can you elaborate on why do you think you need to use a dot in the identifiers?


i92guboj 05-06-2009 08:05 PM

As parameters?

Code:

./myprog "$foo" "$bar" "$whatever_else"
Or using a file. I can't tell you much without knowing more about the program that's going to receive the data.

dnwake 05-06-2009 08:31 PM

The external process wants to receive the values as environment variables of the form foo.bar=123. I have no control over this.

I am just trying to find a way to invoke the external process from BASH. Maybe there is some other way to do it -- Perl perhaps?

Cheers,


Quote:

Originally Posted by i92guboj (Post 3532741)
As parameters?

Code:

./myprog "$foo" "$bar" "$whatever_else"
Or using a file. I can't tell you much without knowing more about the program that's going to receive the data.


i92guboj 05-06-2009 09:01 PM

Quote:

Originally Posted by dnwake (Post 3532764)
The external process wants to receive the values as environment variables of the form foo.bar=123. I have no control over this.

I am just trying to find a way to invoke the external process from BASH. Maybe there is some other way to do it -- Perl perhaps?

Cheers,

It doesn't matter if you use perl or a compiler designed in the NASA. Bash just doesn't allow dots in the names of identifiers, so, if the program is truly reading variables from your shell environment using getenv() or a similar function and it expect them to hold dots in the name of the identifier, then the program is just plain wrong from the root and should be changed. The other alternative is to patch bash in all your machines, and maintain it yourself. Not a pleasant one.

No shell that I know of (including bash, the plain posix bourne shell -sh-, and all the csh variants) will allow that. I don't know anything about ksh though, so I won't speak about it.

Are there any instructions that we can take at a look at or something? I really can't believe that the program has been done in such way because no shell that I know of can define identifiers which contains dots. Only A-Z,a-z,0-9 and _ are allowed, and the first character can't be a cypher.

exussum 03-23-2010 09:52 AM

Quote:

Originally Posted by i92guboj (Post 3532777)
No shell that I know of (including bash, the plain posix bourne shell -sh-, and all the csh variants) will allow that. I don't know anything about ksh though, so I won't speak about it.

I keep bumping into this post from google.

Actually, csh and tcsh support dots in their variable names.

Code:

[exussum:/] exussum% setenv A.B C.D
[exussum:/] exussum% setenv A.C D=F
[exussum:/] exussum% env | grep 'A\.'
A.B=C.D
A.C=D=F

in c, an env is read in via, "int main(int argc, char **argv, char **envp)". I'd guess it's backwards compatibility w/ sh or ksh why bash does this.

dgwsoft 03-28-2010 01:05 PM

environment variables names _themselves_ can contain any printable character except '='

http://www.opengroup.org/onlinepubs/...bd_chap08.html

But various shells restrict the characters you can use. I have been bitten by this. See here, particularly catkin's suggestion:

http://www.linuxquestions.org/questi...3/#post3915600

In C/C++ you would need to use setenv, e.g.

setenv("X.X", "PQR", 1);

and then fork() to launch your process that uses the variables.


All times are GMT -5. The time now is 04:22 AM.