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

hellojoshhhy
  • 855
  • 2
  • 15
  • 32
  • 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? – n. m. could be an AI Aug 25 '18 at 06: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 – hellojoshhhy Aug 25 '18 at 06: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. – n. m. could be an AI Aug 25 '18 at 06: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. – cdarke Aug 25 '18 at 07:21

0 Answers0