I want to put below declare in a shell script: proxy_set
declare -x https_proxy="https://192.168.220.4:8080/"
And then I execute it like below.
$ ./proxy_set
But “export” shows nothing happened.
And in another way if I execute it like this:
$ source proxy_set
Then “export” shows it works!
My question is how can I make it work without additional “source” cmd?
Thanks!
You can’t. Setting variables in the environment only affects the environment of that shell and any future children it spawns; there’s no way to affect the parent shell. When you run it without the
source(or.), a brand new shell is started up, then the variable is set in that shell’s environment, and then that shell exits, taking its environment with it.The
sourcereads the commands and executes them within the current shell as if you had typed them.So if you want to set environment variables in a script, you have to
sourceit. Alternatively, you can have a command generate shell commands as output instead of running them, and then the parent can evaluate the output of the command. Things likessh-agentuse this approach.