Bash: Enter O2 env and then execute second command in the same env

Hello there,

I am writing a C++ Tool that requires me to execute some bash command in the O2 environment.

The issue that I’m having is that whenever I run sth like “alienv enter O2/latest && echo $ROOTSYS” in the shell, then the first part of the command is executed (the O2 environment is entered) but the second part (echo $ROOTSYS) is never executed because the shell waits for the first command to finish. Only as soon as I manually exit the O2 environment, the second command is executed (but then it won’t work since I am not inside the O2 env anymore (the echo $ROOTSYS is just an arbitrary example for this post).
Does anyone know what I can do to enter the O2 environment and then execute a second bash command in that same O2 environment, preferably in a single bash command so that it can be easily implemented in C++? If that is not possible: How can I work around this issue using C++?

Any help is appreciated, thanks for any replies!

Best regards,

Hi @felix!

You can use alienv load in a subshell instead, like so:

(eval "$(alienv load O2/latest)" && echo "$ROOTSYS")

The alienv load command prints variable assignments in your shell’s syntax on stdout, which you can eval.

If this is part of a larger bash script, consider running this in a subshell (as I did in the snippet above by surrounding the command with parentheses ()), so that the variables set by the alienv load don’t persist for the rest of the script.

If this is the only command you’re running, then you can leave out the subshell to avoid an extra fork(), of course. In the latter case, a full bash command might be:

bash -c 'eval "$(alienv load O2/latest)" && echo "$ROOTSYS"'

Run alienv --help and/or modulecmd --help to see a list of commands. Perhaps some of those will be useful to you.

Thank you very much, that’s exactly what I was looking for