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.
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.
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.
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