Home > Blockchain >  Passing arguments to bash command after eval "$(conda shell.bash hook)"?
Passing arguments to bash command after eval "$(conda shell.bash hook)"?

Time:02-26

While creating a conda environment and running some python code in it, from a bash script, I was experiencing some difficulties in passing a variable to the command.

Example 1

This is the hardcoded command which does work.

eval "$(conda shell.bash hook)"
cd some-repository && conda deactivate && conda activate some-environment && python -m code.project1.src --some-arg

MWE

Here is an example that does not process the variable in the command:

# bash -c "some_test"
some_test() {
    echo "PWD=$PWD"
    eval "$(conda shell.bash hook && export env_var=some-dir)"
    cd $env_var
    echo "PWD=$PWD"
}

Which outputs:

PWD=/home/name/git/some-repo
PWD=/home/name

Question

How can I pass variables to the commands that are being executed after eval "$(conda shell.bash hook)"?

CodePudding user response:

eval "$(command)" executes the output of the command line. So you need to output the variable assignment, not execute it in the command.

You're executing the variable assignment in the subshell of $(), so none of its variable assignments persist into the calling shell.

eval "$(conda shell.bash hook && echo export env_var=some-dir)"

But I'm not sure why you need to do that inside the $(). You can just write

eval "$(conda shell.bash hook)"
export env_var=some-dir
  • Related