1

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)
Keith
  • 133

1 Answers1

0

If you're using bash, then the short answer is to put your exported variables into one of the existing personal startup (or initialization) files. Variables exported in your terminal get lost when you close it. Variables exported in your script get lost when your script exits.

There are other startup files which may or may not exist, but these are the most common ones:

  • ~/.bashrc
    • executed by bash for non-login shells
  • ~/.bash_profile
    • executed by bash for interactive login shells
    • ~/.profile is not read if this file exists
    • also reads ~/.bashrc on some systems
    • needs a (logout and) login to reread the file
  • ~/.profile
    • executed by the command interpreter for login shells
    • is not read when ~/.bash_profile exists
    • also reads ~/.bashrc on some systems
    • needs a (logout and) login to reread the file

Or you create a new startup file with all your personal customizations and read it (source it) with source /path/to/myenvironment or . /path/to/myenvironment either on the command line / in your scripts or in one of the startup files mentioned above.

Some example startup files are in package bash-doc in Ubuntu/Debian (install with sudo apt-get install bash-doc) and are installed to /usr/share/doc/bash/examples/startup-files/

Freddy
  • 25,565
  • Thanks but I am using EC2 so I will only go into this machine once. I want to have one setup script. I have read the posts erroneously marked as duplicate and I think the answer is to execute the script with 'sudo source test.sh' – Keith Mar 03 '19 at 02:40
  • @Keith As environment files usually don't install things, you could put all your stuff into test.sh, make it executable and run it with sudo ./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 file myenv and run sudo bash -c ". /path/to/myenv; /path/to/test.sh" (or sudo sh -c "..." for dash). If you export the path to your script in myenv, you could also write sudo bash -c ". /path/to/myenv; test.sh". – Freddy Mar 03 '19 at 06:48
  • test.sh is not an environment file I just want one line in it to set up my environment during the install process. I have already tried all the things you mention but after executing the path is not set – Keith Mar 04 '19 at 16:54
  • @Keith You can't set environment variables in a script. As soon as the script exits the variables are gone. You have to source an environment file before that, either automatically (~/.bashrc etc.) or manually. – Freddy Mar 04 '19 at 16:58
  • Don't tell me what I can't do. :P This works "source <(sudo cat test.sh)" Now I can set up and install everything in one big script on EC2. If you can update your answer to say why only this hackish way to do it works I'll accept it. – Keith Mar 04 '19 at 17:45
  • @Keith Of course you can source a script, but usually environment files don't end with ".sh" and are not used to install things. But it works... You can also use source test.sh if you have permission to read it. – Freddy Mar 04 '19 at 17:53
  • Did not have permission. Found the answer here https://unix.stackexchange.com/questions/202332/sudo-source-command-not-found – Keith Mar 04 '19 at 18:12