I need to set some environment variables in Ubuntu. I do the following and it works:
export PATH="/home/vagrant/ns-allinone-2.35/bin:/home/vagrant/ns-allinone-2.35/tcl8.5.10/unix:/home/vagrant/ns-allinone-2.35/tk8.5.10/unix:$PATH"
export LD_LIBRARY_PATH="/home/vagrant/ns-allinone-2.35/otcl-1.14:/home/vagrant/ns-allinone-2.35/lib"
export TCL_LIBRARY="/home/vagrant/ns-allinone-2.35/tcl8.5.10/library"
But I move the same thing in a script envexport.sh and execute it, the environment variables are not getting set.
Where am I going wrong? How to accomplish this?
Thanks.
If you just run the script, the environment variables get destroyed when the script finishes.
Use . envexport.sh. That way the commands get executed in the current shell (environment).
When you run a command in the shell, the shell creates a subprocess (child process). All the environment variables which were defined or changed down in the subprocess will be lost to the parent process.
However if you source a script, you force the script to run in the current process. That means environment variables in the script you ran will not be lost.
One thing that may help is if you will want those variables set for all of your sessions you can place the same commands in your .bashrc file by running the following command and pasting the lines in the file.
vim ~/.bashrc
and the run
source ~/.bashrc
in any terminals you currently are running. If you start any new terminals they will automatically have your directories added to your path.
Related
I have a bash script where I make a few changes to the .bashrc. I then want to run the bashrc from my script so I've been running
source ~/.bashrc
to avoid having to reload my shell. The problem I've been seeing is that it's only being set in the subshell bash is running in.
Can I do anything from my script so that the source command is run in the parent shell?
What you could do, if you really wanted to: Provide a shell function which checks whether .bashrc has been modified, and if it is the case, sources this file. Let#s call this function check_reload. Then define your PS1 as
PS1='$(check_reload) .....'
With this setup, your .bashrc will be reloaded before you get a new command line.
While this should solve your problem, I personally would not do it: I consider the information in .bashrc fairly static, and I would not use a script to modify it, but do it manually with a text editor. But of course everyone can do this as he likes....
I've written a shell script on my Mac that runs fine from the folder. Trying to make it globally executable I've used the following script:
export PATH="$PATH:~/scripts"
Subsequently, I can run the command blaster from any folder. However, if I close my terminal window, it seems that the PATH gets lost and I have to run the original command again. Any idea why that export PATH needs to be re-established?
No they won't
Because your current export is retained in the current shell you were running the scripts from. As soon as the shell is terminated, the exported variables loose their scope. Add a line
echo 'export PATH=$PATH:~/scripts' >> ~/.bashrc
To make the changes permanent, add the line in either of .bashrc/.bash_profile or .profile depending upon your login shell. These files are read and sourced ( executed in the current shell) before your prompt appears, and from subsequent point you can call your script directly
I have a shell script that exports a couple of environment variables that are needed for building a software project (Android Keystore Location).
Usually when I call the script the environment variables are exported, and the IDE can access them, so does the export command on the Bash Terminal.
Since I installed Mac OS X El Capitan, the environment variable set by the Bash command
export FOO="bar"
are not returned when I try to access them by
echo $FOO
on the shell. Instead I only get a empty line returned.
If I use printenv from within the shell script $F00 is displayed.
When I call printenv from the Cash terminal $FOO is missing.
I read that the OS X "El Capitan" updates fixes some security issues concerning bash. Could that be the cause ?
You cannot modify or export variables in the shell with a shebang script (a script whose first line begins with #! and the pathname of an executable), if that's what you're doing, because that creates a separate process to execute the script and any variables it exports are only visible to its child processes.
You can only arrange for a script to modify variables in the current shell by executing the script within the current process with . (or source) for example.
For example, the ~/.bashrc, ~/.bash_profile and ~/.bash_logout scripts are executed directly by the shell, so they can set or export variables to be inherited by commands and sub-shells run from the shell.
I am having a similar issue. It seems that environment variables are not being propagated to child processes from bash. I solved it by explicitly adding the variables I need to the child.
export MYVAR=foo
MYVAR=$MYVAR ./executable_to_launch
I would be interested to see if someone has found a better solution.
Environment variable declared in /etc/profile:
export MYNAME=rhel
Content of script which is running from monit is [/tmp/printmyname.sh]:
echo "My Name is: "$MYNAME >> /var/log/env_variablefile.out
Content of monit:
check file PrintVariable with path /var/log/something.out
start program = "/bin/sh /tmp/printmyname.sh"
if timestamp > 1 minutes then start
I want to print environment variable declared in /etc/profile to /var/log/env_variablefile.out when /var/log/something.out file is not updated since one minute.
So my problem is when i directly run /tmp/printmyname.sh it append My Name is: rhel into /var/log/env_variablefile.out but when it is running from monit it only prints My Name is:.
So I want to know the reason of such behavior and a way to solve problem.
Note: monit is running every 10Seconds and above code is just example of my actual code.
/etc/profile is only executed for interactive shells.
One way to fix this is to add this to the beginning of /tmp/printmyname.sh:
. /etc/profile
Note that this may cause problems because /etc/profile tries to set up an interactive environment, so a lot of setup scripts will be called that you may not want.
A better solution is probably to put this variable in a new global script and source this new script from both /etc/profile and /tmp/printmyname.sh
Related articles:
Execution sequence for .bash_profile, .bashrc, .bash_login, .profile and .bash_logout
Bash and Its Startup File Execution Algorithm
/etc/profile not being sourced
We have few executable which need some environment setting.
We manually running those scripts before running the executable
Like
$ . setenv.ksh
We have to encompass call these in one script to avoid the manual work.
We written a sh script like
#!/bin/sh
. setenv.ksh
./abc &
Still the environments are not setting in that session. I think the “. setenv.ksh” runs with fork and it’s not setting the environment.
Please me to solve this problem. Which command we use to run the setenv.ksh so, this will work fine.
Thanks
I notice the environment script is called setenv.ksh but you try to run it from /bin/sh. Maybe your system has a shell other than ksh as /bin/sh and it misparses something it setenv.ksh. Try changing the shebang line to #!/bin/ksh (or whatever the path to ksh is on your system).
In setenv.ksh, you need to export all environment variables you set so that any sub-shell will inherit the values:
export MYENV=myValue