views:

12

answers:

2

when I at company , I have to export 3 enviroment variables, http_proxy,https_proxy,all_proxy,

I wrote a file ~/bin/setproxy like this

#! /bin/sh
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888

but when I execute this file at bash, then use env | grep http_proxy , I got nothing. but "source ~/bin/setproxy" works, but is there any way short this to 1 word command. I wrote another file only 1 line,

source ~/bin/setproxy

but it does not work.

+2  A: 

When you execute that script a sub-shell is spawned and the three export are perfomed in that shell, when the script finishes, the sub-shell exits, that's why you don't see the environment variables as set.

You could put that code in a function, say in your .bashrc, and call that, this way it will work, something like the following:

function setproxy {
    export http_proxy=http://......:8888
    export https_proxy=http://......:8888
    export all_proxy=http://......:8888
}
Alberto Zaccagni
+1  A: 

I think your problem is because you are executing either:

~/bin/setproxy

or:

your_other_file_which_sources_setproxy

In both those cases, they run in a subshell which means the export is in that subshell, not the shell you're calling them from.

You can either use the short form of source:

. ~/bin/setproxy

or create an alias:

alias sp='source ~/bin/setproxy'

in your .bashrc or other startup scripts.

That latter solution will allow you to just execute:

sp

to do the work.

paxdiablo