I am trying to put my whole environment set up into a single bash file. I am only hitting one issue. When I try to export a new path from inside the file it is not setting the path in the environment that the bash file was executed in. I want to do
>>sudo -H sh test.sh
Where test.sh is
bunch of installs
export PATH=/home/ubuntu/anaconda3/bin:$PATH
a second bunch of installs
I thought this was the purpose of eval so I tried
eval `export PATH=/home/ubuntu/anaconda3/bin:$PATH`
and even
eval `echo "export PATH=/home/ubuntu/anaconda3/bin:$PATH"`
but I can't access the command in the terminal. If I do it manually it works.
---------------UPDATE------------
The standard bash execution completes in a sub-shell so the environment variables are lost after execution. If you want to execute in the same shell then you
source test.sh
However, I need to be able to have full permissions in the execution so I need to use sudo. As explained here you cannot call sudo source but they did provide a hack to get it to work
source <(sudo cat /etc/environment)
test.sh
, make it executable and run it withsudo ./test.sh
. Don't forget to add#!/bin/bash
(bash) or#!/bin/sh
(dash) as first line to your script. Or if you want an additional environment file, then put all your exported paths into filemyenv
and runsudo bash -c ". /path/to/myenv; /path/to/test.sh"
(orsudo sh -c "..."
for dash). If you export the path to your script inmyenv
, you could also writesudo bash -c ". /path/to/myenv; test.sh"
. – Freddy Mar 03 '19 at 06:48~/.bashrc
etc.) or manually. – Freddy Mar 04 '19 at 16:58source test.sh
if you have permission to read it. – Freddy Mar 04 '19 at 17:53