Set Environment Variables for Non-Interactive Shell

Set environment variables for non-interactive shell

Solution for Ubuntu: set the variables in /etc/environment, and it works for all users and all types of shells.

How to set environment variables so they work in non-interactive bash shells?

After digging around on the Googles - I believe there is no standard Linux way to set an environment variable for non-interactive shells.

But there is a Docker way to answer the question. On the original docker create of the container from the amazonlinux:2 image, specify the environment variable via -e JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64. This stores the environment variable in the docker metadata for the container and it will be available in all execution contexts, including non-interactive shells invoked directly via docker exec (without having to specify it explicitly for every exec command).

As far as I know, this is the same as what the ENV command in a Dockerfile does.

How to set environment variable for non-interactive shell in docker container?

I finally understood the specific meaning of the relevant answers

It means, in the remote VM or container, create a linux script file named mypython as the python wrapper with the content:

#!/bin/bash -l
/path/to/interpreter/bin/python "$@"

, where /path/to/interpreter/bin/python is the path to the python interpreter. For conda interpreter, it might look like /root/miniconda3/envs/py37/bin/python.

The script mypython should be placed in the same path as binary python, i.e., /root/miniconda3/envs/py37/bin/mypython

And then add the execute permission to mypython:

chmod +x /root/miniconda3/envs/py37/bin/mypython

The above two steps can also be executed by command as an alternative:

echo '#!/bin/bash -l
/root/miniconda3/envs/py37/bin/python "$@"' > /root/miniconda3/envs/py37/bin/mypython
chmod +x /root/miniconda3/envs/py37/bin/mypython

At last, add the SSH interpreter in Pycharm, make sure the interpreter path is /root/miniconda3/envs/py37/bin/mypython

And the problem is solved.

Correct way to source .bashrc for non-interactive shell

maybe you should run like this.I guess.
two ways help you!

first:

mpirun --prefix /home/usama/.openmpi --hostfile hosts -np 4 . ~/.bashrc && bash script

second:

## 1. add this line to the script
. ~/.bashrc

## 2. run command as you do
mpirun --prefix /home/usama/.openmpi --hostfile hosts -np 4 bash script

How does the two differ: Declaring an environment variable at the same line as the command vs. declaring the env. var. and then run the command

I'm assuming you meant LD_LIBRARY_PATH, but either way, the answer is the same:

There are two types of variables that are relevant in this context:

  1. Non-environment variables, which are set in the current shell's context and do not persist into subshells. Sometimes these are called "shell variables" or "local variables", but those terms can have other meanings.
  2. Environment variables, which are set in the current shell and persist into the context of subshells spawned from the current shell. They can be displayed with the "env" command.

The first (one-liner) command in your post sets LD_LIBRARY_PATH as an environment variable, but only for the invocation of a.out that's on the same line. a.out runs in a subshell and can see the value of LD_LIBRARY_PATH (because it's an environment variable). After a.out finishes, the LD_LIBRARY_PATH that was set goes out of scope.

The second set of commands sets LD_LIBRARY_PATH as a non-environment variable, which means the variable is available in the current shell only. Because a.out runs in a subshell when you call it, it won't see the value of LD_LIBRARY_PATH. You would need to precede the LD_LIBRARY_PATH=<path_to_lib> with an "export" to make it an environment variable, as shown below.

$ cat ./a.out
#!/bin/bash
env | grep LD_LIBRARY_PATH
echo $LD_LIBRARY_PATH

# LD_LIBRARY_PATH is treated as an environment variable
$ LD_LIBRARY_PATH=/usr/lib ./a.out
LD_LIBRARY_PATH=/usr/lib
/usr/lib

# LD_LIBRARY_PATH is NOT set as an environment variable
$ LD_LIBRARY_PATH=/usr/lib
$ ./a.out

# "export" sets LD_LIBRARY_PATH as an environment variable
$ export LD_LIBRARY_PATH
$ ./a.out
LD_LIBRARY_PATH=/usr/lib
/usr/lib
$


Related Topics



Leave a reply



Submit