2

I have a bash script to write proxy into /etc/profile and source it. This is the snippet of the script

#!/bin/bash
configureEnv(){
        cat >> /etc/profile <<EOF
export SOCKS_SERVER=http://proxy.mycompany.com:1080
export HTTP_PROXY=http://proxy.mycompany.com:911
EOF
        source /etc/profile
}
configureEnv

I run this script on the terminal with command ./test.sh The proxies are added into /etc/profile but when i run env | grep proxy, there is nothing until i run source /etc/profile on the terminal. My terminal is using -sh

Any idea? Thanks

4
  • 2
    Running your script won't update your environment. A running script updates its environment. You need to source the script. Why do you think running /etc/profile doesn't work? Commented Aug 25, 2018 at 6:29
  • @n.m. What do you means "running" /etc/profile? Any solutions I can improve my script because after this function, my script will download somethings outside the firewall, and I need the proxy take place Commented Aug 25, 2018 at 6:32
  • 1
    I don't know how to explain "running" in simple words. Execute as a separate process? You are running your own script so you should probably have a rough idea of what running is. If you have a question about downloading things with your script, you better ask a question about downloading things with your script, because it's not the same question as the question about getting proxy variables in your environment. Commented Aug 25, 2018 at 6:39
  • 1
    "there is nothing until i run source /etc/profile on the terminal" - correct. That's how shell processes work, and that's how they are meant to work. When you do a source inside another script you only change that child process's environment, it does not, and cannot, change the parent. Commented Aug 25, 2018 at 7:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.