I have a bash script I'm running with sudo that is supposed to call three other bash scripts. Two of the scripts that are called run fine. One of them, however, attempts to call a python script. The path to this python script is given in the script as SCRITPS_URI="${HFGEO_HOME}/bin/waveform.py." My problem is that despite trying many different approaches, I can't make the third script properly expand $HFGEO_HOME. I have tried:

exporting it

running with sudo -E bash -c

running sudo HFGEO_HOME=/data/hfgeo/ ./script.sh https://askubuntu.com/questions/57915/environment-variables-when-run-with-sudo

adding it to my ~/.bashrc and my ~/.bash_profile

Running sudo visudo and adding it to my defaults env_keep https://askubuntu.com/questions/57915/environment-variables-when-run-with-sudo (Second to last answer)

I keep getting the error python: can't open file '/bin/waveform.py': [Errno 2] No such file or directory I have confirmed that the file is where it is supposed to be. In my bashrc and bash_profile files I am exporting the variable as:

export HFGEO_HOME=/data/hfgeo

and then add it to my PATH variable as

export PATH=${PATH}:${HFGEO_HOME}/bin

What's bugging me is that the system I'm trying to set up is running on the same OS, albeit a newer version, as another machine I have where this script works just fine. The original machine is running CentOS 6.4 and the machine I am having this problem on is running on CentOS 6.9. I did not have to do any of the above to get this script to work on the 6.4 machine. I've already tried fixing this myself and some of the above attempts I made were based on my research, but none of them seemed to fix my problem. What am I doing wrong?

  • What if you replace script.sh with env? i.e. sudo HFGEO_HOME=/data/hfgeo/ env => Does HFGEO_HOME appear in the output?
    – xhienne
    Sep 14, 2017 at 23:36
  • sudo env VARIABLE=value cmd.
    – Kusalananda
    Sep 17, 2017 at 11:51
  • Can you post the full script?
    – nxnev
    Sep 17, 2017 at 12:48
  • Double-check if it's really defined as SCRITPS_URI?
    – mtklr
    Sep 17, 2017 at 14:42
  • I tried the above two solutions from @Kusalananda and @xhienne but no luck. It is really defined as SCRITPS_URI I double checked
    – nb12345
    Sep 18, 2017 at 13:48

1 Answer 1


Have you tried executing the script from inside of a sub-shell? Something like this should work:

sudo bash -c 'export HFGEO_HOME=/data/hfgeo/; ./script.sh'

It would help if you posted the script that you're trying to run, or preferably a minimal example that produces the same error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .