shell export variable not come into effect - linux

I (on mac osx) often use
export http_proxy=http://192.168.0.205:1099
to proxy http connection to get a highed download speed. To make things easy, I wrote a shell file named proxy.sh to do this:
#!/bin/sh
export http_proxy=http://192.168.0.205:1099
Before I downlaod, I execute proxy.sh shell command, but I found it did't not come into effect.It lost http_proxy variable in current commnad window(terminal). I must type export command in current terminal,it will come into effect.
So I want to know what's reason for this and a solution? thanks.

Running a shell script "normally" (with proxy.sh for example) results in that running in a sub-process so that it cannot affect the environment of the parent process.
Using . or source will run the shell script in the context of the current shell, so it will be able to affect the environment, using one of the following:
. proxy.sh
source proxy.sh
Another possibility (if you're using bash at least) is to create an alias to do the work for you. You can use something like:
alias faster='export http_proxy=http://192.168.0.205:1099'
so that you can then simply type faster on the command line and it will export that variable (in the context of the current shell).
You could also allow for one-shot settings such as:
alias faster='http_proxy=http://192.168.0.205:1099'
and then use:
faster your_program
which would translate into:
http_proxy=http://192.168.0.205:1099 your_program
That's a bash way to set a variable for just the one invocation of a command.

The export variable will only apply to the script -- if you want it to apply to the shell, you need to use source, and execute the script like so:
. ./proxy.sh
or:
source ./proxy.sh
Note the "." in the first example -- the dot follow by space means the script will apply to the shell.

The reason why your script does not work has been explained by Drakosha & how to make your script work has been explained by Anothony. But with the export in the script you need to source your script each time you open a new terminal. A better solution will be to add the export in .bash_profile or .bashrc
Hope this helps!

When executing a shell script a new shell is launched, the script is executed, and the shell dies. That's why you don't see the variable defined in your shell.
I suggest using an alias for the same purpose.

Related

How to set java environment variables using shell script

I know I can just type
$ vi .bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
However how to use shell script to do it? I prefer to write shell script because I need to configure multiple servers, if type one by one I would take me a long time to go.
Can someone guide me how to do this? Thanks a lot!
EDIT: I just realized that you wanted a script to automate the process of adding environment variables. These commands may work for you:
echo "export JAVA_HOME=/usr/lib/jvm/java-7-oracle" >>~/.bashrc
echo "export PATH=$JAVA_HOME/bin:$PATH" >>~/.bashrc
What this does is append the given text to .bashrc. Instead of copying .bashrc files from server to server, run these commands (you could probably write a script for these) on each server. This preserves the contents of the original rc files on each server, which I find is a better idea than completely overwriting the file.
Original answer
Your .bashrc file is actually written as a shell script. You would place the exact same lines in the shell script, possibly with a hashbang at the beginning of the file. For example:
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
# do java stuff here...
If you were to "do java stuff" in this script, this would work fine. However, if these variables are going to be used outside of the script, you would have to "source" this file. That is what happens with .bashrc. Before the first prompt is given, Bash runs source ~/.bashrc to publish the variables defined in .bashrc.

Linux Environment Variables

I'm trying to change my environment variables using a shell script, but the shell script isn't changing it.
Sorry for asking that type of question, but I'm learning about Linux, I just can't figure out what I'm doing wrong here..
Here's the script
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/jre/bin
After I execute this .sh file, when I type
echo $PATH
It doesn't return the new PATH I setted
Instead of executing it like
$ ./script.sh
source it with
$ source script.sh
The first variant creates a new process which does not pass back its modified environment variables to the calling process, while sourcing just executes the script within the already running shell.
See also What is the difference between executing a bash script and sourcing a bash script?
As Andreas explained, you need to source the definition (and you need the export to happen in your current shell process, not in a subshell or child process)
You could instead define in your ~/.bashrc some shell functions to do the job:
## remember the original path at start of the interactive bash
export ORIGINAL_PATH=$PATH
function prepare_for_java() {
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60
export PATH=$ORIGINAL_PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
}
function forget_java() {
unset JAVA_HOME
export PATH=$ORIGINALPATH
}
then in your terminal you would type
prepare_for_java
before starting doing Java things, and
forget_java
after you've done with them.
I would suggest to read the Advanced Bash Scripting Guide and Advanced Linux Programming (to get a wider picture).
See also bash(1), credentials(7), fork(2), execve(2), environ(7)

Linux: export environment variable in a shell script to make it flexible on any server

In order to run a Tcl script on Linux, I need to set the environment variable "$LD_LIBRARY_PATH" each time.
For convenience, I develop a shell script to do this.Currently, on my own server, if I type
echo $LD_LIBRARY_PATH
the result is:
/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib
so in my shell script I write the following code:
export LD_LIBRARY_PATH="/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
where the "$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64" part is what I want to add. It works well. Now the issue is:
If I want to run the script on any server, so the original "$LD_LIBRARY_PATH" will be different, based on my understanding. So how to make it flexible on any server?
I try this in my shell script:
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
But not so sure,
I am new to system stuffs, need some help. Hope explain the issue clearly.
If your default shell is bash, i would define the variables in ~/.bashrc like:
export INSTALL_ROOT=...##assuming a lready defined
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64
So in this way, you dont have to worry anything about setting variables in multiple shell scripts as this .bashrc is going to setup variable for you beforehand.

How to make declare in a Linux shell script?

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 source reads 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 source it. 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 like ssh-agent use this approach.
Try just adding:
export https_proxy="https://192.168.220.4:8080/"
Then execute your script normally.

How do I import environment settings into my Perl program?

I have a script whose content simply exports a variable in linux.
export LD_LIBRARY_PATH=....
I want to run this script in my Perl script so whoever is running my Perl script will have their LD_LIBRARY_PATH set. Can i just do this in the beginning of my Perl script:
#!/usr/bin/perl -w
system(". /myfolder1/myfolder2/myScript.sh");
#!/bin/sh
. /myfolder1/myfolder2/myScript.sh
exec perl -wxS "$0" "$#"
#!/usr/bin/perl -w
# .. the rest of your script as normal
When you run this, it will first be executed by /bin/sh, which is capable of loading myScript.sh into the local environment. sh then execs Perl, which is told to continue from the following line.
This won't work. To change the environment inside your Perl script (and to change the environment that will be passed on to commands run from inside your Perl script), change the %ENV variable.
$ENV{"LD_LIBRARY_PATH"} = ... ;
This won't work. There is no way for a subshell to manipulate the environment of the parent process.
But you could make your script echo the string you want to set as LD_LIBRARY_PATH and then from within your Perl script you could do something like that:
$ENV{LD_LIBRARY_PATH} = `path/to/your/script.sh`;
Of course, a bit of error checking might also be a good idea.
No. Your environment changes made in a child cannot affect the parent. This means running a script will not affect perl. Also perl will not affect the shell from which it was called. You can edit the environment inside perl by changing the special variable %ENV. If there's some kind of unreproducible calculation done in that script, maybe the script should just echo the setting and perl can pick that up on STDOUT and use it.
I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?
Unix In the strictest sense, it can't be done -- the script executes
as a different process from the shell
it was started from. Changes to a
process are not reflected in its
parent, only in its own children
created after the change.
I had a similar problem a few years ago and whipped up a little module, Env::Sourced, that should do the trick.
use Env::Sourced qw(/myfolder1/myfolder2/myScript.sh);
...
Another option (other than making the changes directly in Perl's %ENV) is to make the changes you want a Perl module, so that you can say:
use MyEnvironment;
and have it modify your environment in all your scripts. It would make it simple to make changes after the fact that will not require editing every script.
The module itself will be simple, something like this:
package MyEnvironment;
$ENV{LD_LIBRARY_PATH} .= ":/some/path/you/want/appended";
# Any other changes you want here.
1;
That won't work. An (unpleasant) alternative might be to replace /usr/bin/perl with a shell script that first executes your script and then executes the perl executable.
This can't be done in the way you're trying to do this.
It either needs a wrapper shell script that sets LD_LIBRARY_PATH and then calls your perl script, or any user executing the script needs to have LD_LIBRARY_PATH set correctly in the first place.
If doing the latter, then this can be managed globally by editing /etc/profile and /etc/cshrc (for ksh, sh, bash, csh and tcsh) shells. You can then test for the value of LD_LIBRARY_PATH in your script and if not set/set incorrectly then print a friendly message to the user. Alternatively individual users can set this in their local .profile/.cshrc files.
Note: you haven't given any information about the environment or useres that might run this, so there's also the possibility that users may set LD_LIBRARY_PATH to something they need. If you do check LD_LIBRARY_PATH for a "good" value in your script, then keep in mind that several paths may have been specified, so you will need to parse this environment variable properly.
If you can find the right place in your perl script, this works as in my example:
$ENV{"LD_LIBRARY_PATH"} = "/oracle/product/10g/lib";
And it didn't require me to call another script to set the env var.
The Env::Modify module addresses this issue, at least for POSIX-y platforms:
use Env::Modify 'source';
source("/myfolder1/myfolder2/myScript.sh");
... environment settings from myScript.sh are now available to Perl ...

Resources