Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create environment variable with dot in the current environment [duplicate]

Tags:

bash

I know that you can create environment variables with the command env.

For example:

env A.B=D bash

The problem is for env a command is required thus creating a new subprocess.

like image 225
razpeitia Avatar asked Apr 20 '15 16:04

razpeitia


People also ask

Can environment variable have dots?

It is not possible to set environment variables with dots in their name. If you try to set one in the UI, it just does not work without error message.

How do I set environment variable in CMD?

2.2 Set/Unset/Change an Environment Variable for the "Current" CMD Session. To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.

How do I set an environment variable permanently?

To make permanent changes to the environment variables for all new accounts, go to your /etc/skel files, such as . bashrc , and change the ones that are already there or enter the new ones. When you create new users, these /etc/skel files will be copied to the new user's home directory.


2 Answers

Bash does not allow environment variables with non-alphanumeric characters in their names (aside from _). While the environment may contain a line such as A.B=D, there is no requirement that a shell be able to make use of it, and bash will not. Other shells may be more flexible.

Utilities which make use of oddly-named environment variables are discouraged, but some may exist. You will need to use env to create such an environment variable. You could avoid the subprocess with exec env bash but it won't save you much in the way of time or resources.

like image 155
rici Avatar answered Sep 24 '22 22:09

rici


rici has the correct answer. To demonstrate the difference required to access such environment entries, bash requires you to parse the environment as text:

$ env A.B=C perl -E 'say $ENV{"A.B"}'
C
$ env A.B=C bash -c 'val=$(env | grep -oP "^A\.B=\K.*"); echo "$val"'
C
like image 32
glenn jackman Avatar answered Sep 23 '22 22:09

glenn jackman