0

I'm working on installzation of oneapi in my pc with CentOS 7 system. After installed the components of oneapi, I want to setup the environment of oneapi by sourcing (in the terminal) the setvars.sh file

source /opt/intel/oneapi/setvars.sh intel64

the terminal echoes multiple lines of output as it should

:: initializing oneAPI environment ...
   BASH version = 4.2.46(2)-release
:: dev-utilities -- latest
:: tbb -- latest
:: compiler -- latest
:: dpl -- latest
:: mkl -- latest
:: vtune -- latest
:: dpcpp-ct -- latest
:: mpi -- latest
:: clck -- latest
:: inspector -- latest
:: itac -- latest
:: oneAPI environment initialized ::

then I can find the location of the compilers like ifort through

which ifort

then it gives

/opt/intel/oneapi/compiler/2021.1-beta10/linux/bin/intel64/ifort

Here is my question,

instead of sourcing setvars.sh directly in the terminal, if I use a sh file like

#!/bin/bash
source /opt/intel/oneapi/setvars.sh intel64

and run the sh file with

bash hello_world.sh

then the outputs still arise as above which I believe can demonstrate that the source command in the sh file had been successfully executed. However, , this time, the environment variables are not probably setted, as which ifort command gives

/usr/bin/which: no ifort in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/wuchen/.local/bin:/home/wuchen/bin)

I noticed this post Difference between sh and bash and changed the command in the sh file to

. /opt/intel/oneapi/setvars.sh intel64

But still it doesn't work. I wonder if my problem is due to the setvars.sh itself.

FaDA
  • 103
  • 1
  • 3
  • 2
    The `source` or `.` commands execute a script _in the context of the **current shell**_. When you run `bash hello_world.sh` you are launching a **new shell**, and that shell will source setvars.sh, but then the new shell exits. Basic linux security: a child process cannot alter the environment of the parent process. – glenn jackman May 16 '21 at 13:09
  • 2
    The solution: don't write hello_world as a *program*, write it as a *function* that executes in your current shell: `hello_world() { source /opt/intel/oneapi/setvars.sh intel64; }` – glenn jackman May 16 '21 at 13:10

1 Answers1

1

Everything you describe show that everything is working the way it should.

The first comment explains the behavior you are seeing. As the comment says, the sourcing of the setvars.sh only affects the current shell and its children, not the parent. Your shell command is in the parent, and will not see the variables set by a child.

You can see the difference if you do 'source hallo_world.sh' instead of 'bash hello_world.sh'

The 'source ...' will not create a new shell, and the variables will be set as you originally expected. The ' bash ...' sets the variables in a child shell, and does not affect the shell you are in.