LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-06-2009, 07:36 PM   #1
dnwake
LQ Newbie
 
Registered: May 2009
Posts: 4

Rep: Reputation: 0
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
 
Old 05-06-2009, 08:03 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Quote:
Originally Posted by dnwake View Post
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?
 
Old 05-06-2009, 08:42 PM   #3
dnwake
LQ Newbie
 
Registered: May 2009
Posts: 4

Original Poster
Rep: Reputation: 0
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 View Post
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?
 
Old 05-06-2009, 09:05 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
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.

Last edited by i92guboj; 05-06-2009 at 09:06 PM.
 
Old 05-06-2009, 09:31 PM   #5
dnwake
LQ Newbie
 
Registered: May 2009
Posts: 4

Original Poster
Rep: Reputation: 0
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 View Post
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.
 
Old 05-06-2009, 10:01 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Quote:
Originally Posted by dnwake View Post
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.
 
Old 03-23-2010, 10:52 AM   #7
exussum
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by i92guboj View Post
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.
 
Old 03-28-2010, 02:05 PM   #8
dgwsoft
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Rep: Reputation: 0
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.

Last edited by dgwsoft; 03-28-2010 at 02:06 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set environment variable yuan13452 Linux - Software 7 09-03-2007 06:23 PM
How to set an environment variable royeo Linux - Newbie 1 12-01-2006 01:59 AM
set environment variable Jongi Linux - Newbie 7 03-01-2004 05:34 AM
Set Environment Variable lloyd_stevens Linux - Software 1 07-30-2003 01:38 PM
How to set a environment variable in rh? yenonn Linux - Newbie 11 02-21-2003 11:24 PM


All times are GMT -5. The time now is 05:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration