You are not logged in.
I'm trying to create a script that will pass environment variables that will stay valid after the script exits.
The problem with this is that (by design?) when executing a script it opens a child environment, and inherits the environmental variables from the parent.
The issue is that the child cannot interact with the parent's environment so setting variables like:
export http_proxy='http://server:port'
wont stay valid once the script exits. I've verified this by trying to echo the variable once the script completes, and i receive an empty reply.
The only work around I have for this is to include this within a bash profile, however I cant just keep adding scripts into my bash profile, because that seems counter-intuitive.
Is there any way to do what I'm trying to do? Or am I just grabbing at straws here?
below is my code for my script, perhaps I've done something wrong...
proxy=x.x.x.x
port=#
username=domain\\username
password=userpassword
export http_proxy='http://$username\:$password@$proxy\:$port'
export ftp_proxy='ftp://$username\:$password@$proxy\:$port'
exit
Last edited by isolatedvirus (2011-10-04 10:58:52)
There are two kinds of light -- The glow that illuminates and the glare that obscures
Offline
You can source your script:
source myscript.sh
#OR
. myscript.sh
That will execute the commands in your current environment, so the variables you set will persist. Just remove the "exit" at the end, as it would end your current shell.
Offline
I tried sourcing my script before, and it logged me out (probably because of the exit at the end of the script)
Removing the exit at the end of the script solved the issue. Thanks for the help!
There are two kinds of light -- The glow that illuminates and the glare that obscures
Offline
One thing to note is that you should specify the sourced file by its path so it includes a slash, quoting bash(1):
source filename [arguments]
Read and execute commands from filename in the current shell environment
and return the exit status of the last command executed from filename.
If filename does not contain a slash, file names in PATH are used to find
the directory containing filename. The file searched for in PATH need
not be executable. When bash is not in posix mode, the current directory
is searched if no file is found in PATH. If the sourcepath option to the
shopt builtin command is turned off, the PATH is not searched
So if sourcepath is set (as by default), you can end up sourcing the wrong file, e.g.
source file
(DON'T do it!)
will really source /usr/bin/file, even if you have 'file' in current directory.
Instead, always specify the path,
source ./file
Keep this in mind or you're in for a big surprise.
Last edited by lolilolicon (2011-10-04 11:33:48)
This silver ladybug at line 28...
Offline