22

I have some proxy settings that I only occasionally want to turn on, so I don't want to put them in my ~/.bash_profile. I tried putting them directly in ~/bin/set_proxy_env.sh, adding ~/bin to my PATH, and chmod +xing the script but though the script runs, the variables don't stick in my shell. Does anyone know how to get them to stick around for the rest of the shell session?

3 Answers 3

27

Use one of:

source <file>

. <file>
Sign up to request clarification or add additional context in comments.

1 Comment

that sir, is money in the bank
23

In the script use

export varname=value

and also execute the script with:

source set_proxy_env.sh.

The export keyword ensures the variable is marked for automatic inclusion in the environment of subsequently executed commands. Using source to execute a script starts it with the present shell instead of launching a temporary one for the script.

1 Comment

I had the export; it was the source I was missing.
8

Did you try this:

. ~/bin/set_proxy_env.sh

Running it by itself opens a separate subshell (I think) and sets the variable there. But then the binding is lost after exiting back into your shell. The dot at the front tells it to run it within the same shell.

Also, don't forget to export the variables you need like so: export MYVAR=value

2 Comments

It's not necessary to export every variable. Only the ones you need to be visible to child processes.
Ah yes, that's what I meant :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.