For an entire day I have been tryng to add an environment variable to linux ad it isn't working. This is the guide that I am following. This is what is written in the .profile file.
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
JAVA_HOME=/usr/local/java/jdk1.8.0_51
JRE_HOME=$JAVA_HOME/jre
LARAVEL=/home/user/.composer/vendor/bin
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$LARAVEL
export JAVA_HOME
export JRE_HOME
export LARAVEL
export PATH
I am running the file. But when I run echo $PATH I am not getting the $LARAVEL path. What am I doing wrong?
Have you logged out and logged in? Also try to update your .bashrc file instead. I think that .profile is only used for the ksh shell.
Try to source the file, like;
$ source yourfilename
In Unix/Linux (unlike in Windows) a program cannot affect the environment of the parent process. When you are running it as a regular shellscript it becomes a subprocess of the parent, and it will affect only it's own environment and not that of the parent.
Using the keyword source (or using the dot .) will instruct your process to executed the shell script directly rather than fork a new subprocess.
echo 'export PATH="$PATH":/path/to/folder/' >> ~/.bashrc
and
source ~/.bashrc
Related
For example, I type to the following command:
# PATH=$PATH:/var/test
# echo $PATH
........./var/test // working
# export PATH
Next, I open another bash shell session to test if the export works by typing the following command:
# echo $PATH
........ // not working as in I don't see /var/test path
you have set the PATH environment variable only for your current bash session. You need to add the line PATH=$PATH:/var/test into ~/.bashrc so that it works for any bash shell.
Just run the following command to put it into your rc(run commands) file (rc files contain startup information for a command(initialization)):
echo "PATH=$PATH:/var/test" >> ~/.bashrc
More info: https://en.wikipedia.org/wiki/Run_commands
https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc
exporting a variable makes it available only in child processes spwaned/started from that bash shell.
As an example:
$ export var=abcd
$ sh
$ echo "$var"
abcd
$ exit
$ echo "$var"
abcd
$
sh is the child process of bash hence it gets the value of var, since you open a new bash which is a different process altogether it does get the PATH value.
This question already has an answer here:
Unable to export the variable through script file [duplicate]
(1 answer)
Closed 6 years ago.
I created a bash script to add
/My_Scripts/Bash_Scripts
to the default PATH of linux.
!/bin/bash
#This Script is used to add a folder/diectory to the PATH..
echo -e "\e[92m\e[1mCREATING PATH...........\n\n"
cd
mkdir My_Scripts
cd My_Scripts
mkdir Bash_Scripts
cd
export PATH=$PATH:$HOME/My_Scripts/Bash_Scripts
echo -e "\e[92m\e[1mPATH CREATON SUCCESSFUL\n \e[39m"
echo $PATH
The output of script is
root#kali:~/Desktop# bash add_path
CREATING PATH...........
PATH CREATON SUCCESSFUL
`/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/My_Scripts/Bash_Scripts'
but if I type echo $PATH in the terminal outside, the path is not added
root#kali:~/Desktop# $PATH
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: No such file or directory
You are starting up a new bash process and PATH is only modified in the context of the new process. When this process quits, changes done in its environment do not propagate to the parent process.
Instead, you would want to modify PATH in the context of current bash process. If you want this temporarily, you may source your script. source will run in the context of the current bash process. Beware of any side effects - like cd will change the directory, exit will terminate the current bash process.
If you want this change permanently for all future interactive sessions, you can modify ~/.bashrc.
Also, the syntax of shebang is #!/path/to/program, you are missing a #.
Your changes is affected in current shell only.Put the entry in .bashrc file. It will affect the all the terminal.open the .bashrc file and add the below line and run the file -
vim ~/.bashrc
export PATH="$PATH:/home/username"
~/.bashrc
Edit the parent shell
script.sh
#!/bin/bash
export "PATH=$PATH:$HOME/My_Scripts/Bash_Scripts"
echo $PATH
$~ PATH=$(./script.sh)
$~ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/loganaayahee/My_Scripts/Bash_Scripts
First thing - you should use echo $PATH. By simply typing $PATH you're trying to execute the command, hence the "No such file or directory error"
Next - the /root/My_Scripts/Bash_Scripts wasn't really added to the PATH. The first output you see in done inside the script, so the changes could be seen there.
The reason is that PATH will be set only in the context of the script shell, execute it as source add_path to preserve the changes in variables (but only for current shell).
If you want the variable to be persistant in all shells - add it to /.bashrc (since you're runnung as root).
I have an application named puppet installed on my Linux box. It is installed at location /usr/test/bin/puppet
This is how .bash_profile looks
export PATH=/usr/test/bin
if I run command puppet apply from console, it works fine but when I call puppet command from inside bash script, it says command not found
#!/bin/bash
puppet apply x.pp
Any ideas on what is wrong ?
.bash_profile is loaded only if bash is invoked as login shell (bash -l or from a real tty), at least in Debian based distributions bash in a virtual tty (for example when using xterm, gnome-terminal, etc...) is invoked as interactive shell.
Interactive shells loads the configuration from ~/.bashrc.
bash manpage:
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
Shellscripts don't load any of these.
You can check which files are opened by any program with strace:
strace ./s.sh 2>&1 | grep -e stat -e open
Possible solutions:
You can export the variable at the beginning of every script:
#!/bin/bash
export PATH=$PATH:...
Or you can have another file with the desired variables and source it from any script that need those:
/etc/special_vars.sh:
export PATH=$PATH:...
script:
#!/bin/bash
. /etc/special_vars.sh
puppet ...
Configure the PATH in in ~/.bashrc, ~/.bash_profile and ~/.profile for the user running the script (sub-processes will inherit the environment variables) to have some warranty that the user can run the script from different environments and shells (some bourne compatible shells others than bash do load ~/.profile)
Maybe the export of PATH is wrong?
export PATH=$PATH:/usr/test/bin/puppet
You could try using an alias, like so
in your .bash_profile:
alias puppet='bash puppet.fileextension'
you can also do
alias puppet='bash path/to/puppet.fileextension'
which will let you run the script from anywhere in Terminal.
EDIT:
OP has stated in the comments that there will be two different systems running, and he asked how to check the file path to the bash file.
If you do
#!/bin/bash
runPuppet(){
if [ -e path/to/system1/puppet.fileextension]
then
bash path/to/system1/puppet.fileextension $1 $2
elif [ -e path/to/system2/puppet.fileextension]
then
bash path/to/system2/puppet.fileextension $1 $2
fi
}
runPuppet apply x.pp
and change the runPuppet input to whatever you'd like.
To clarify/explain:
-e is to check if the file exists
$1 & $2 are the first two input parameters, respectively.
This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 8 years ago.
I am novice for shell scripting. I have written one script which checks if ORACLE_HOME AND ORACLE_SID are set. Inside this I have called other script to set the env variables.
First Script
while [ 1 -gt 0 ]
do
echo -e "Please enter path of oracle home directory:\c"
read DB_HOME
if [ -d $DB_HOME ]
then
./oracle_env.sh $DB_HOME "test1"
echo "ORACLE_HOME has been set successfully!"
status="Y"
break
else
echo "Path or directory does not exist."
fi
done
Second Script
#This script will set ORACLE_HOME and SID
export ORACLE_HOME=$1
export ORACLE_SID=$2
When I run the second script as
./oracle_env.sh /u01/app/oracle test
it's working fine. I mean, when I run
echo $ORACLE_HOME
it gives path like
/u01/app/oracle
Now the problem is when I run the same script from first script, it's not working.
Please help me out !!!
The problem is quite simple:
If you execute a script it starts in a new shell, sets the environment there and close the shell. As result nothing changes in the first calling shell.
So you have to execute the script in the first shell with source <shellscript>
For details see man bash
I have no idea which shell you use. Maybe the solution is a bit different for other shells.
Try this for setting environment variable in your terminal: (Below code is for xampp not for oracle, path will vary w.r.t your requirement)
export PATH=/opt/lamp/bin:$PATH
You can see your environment variables by:
echo $PATH
See, if that works for you.
Run the script with source (or) . command.
while [ 1 -gt 0 ]
do
echo -e "Please enter path of oracle home directory:\c"
read DB_HOME
if [ -d $DB_HOME ]
then
. oracle_env.sh $DB_HOME "test1" ## Here you run the script with . command.
echo "ORACLE_HOME has been set successfully!"
status="Y"
break
else
echo "Path or directory does not exist."
fi
done
Run your first script also with . command.
$ . script.sh
I ran the below script to set environment variables for oracle(oracle_env.sh which comes with oracle package itself).
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
ORACLE_SID=XE
export ORACLE_SID
NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
export NLS_LANG
PATH=$ORACLE_HOME/bin:$PATH
export PATH
if [ $?LD_LIBRARY_PATH ]
then
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$ORACLE_HOME/lib
fi
export LD_LIBRARY_PATH
After that when I ran env to ensure that the variables are exported properly, I found no properties are exported(below is the output).
invincible:/home/invincible# /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
invincible:/home/invincible# env | grep ORACLE_HOME
invincible:/home/invincible#
Now I am not sure whether variables are exported properly.If not what I have done wrong? Please help me out.
And one more thing, I am running as root.
The scripts only sets the environment inside the subshell it runs in. You should source it:
# POSIX
. /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
or
# bash/ksh
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
I believe that when you run a script, bash forks and execs the script in a new shell instance, any exports done in the script doesn't propagate back to your parent shell.
However it seems that you can simply execute your script with:
prompt$ . /path/to/script.sh # note the period!
Example:
prompt$ echo "export FOO=foobar" > /tmp/tst
prompt$ sh /tmp/tst
prompt$ echo $FOO
prompt$ . /tmp/tst
prompt$ echo $FOO
foobar
I believe you should use source to load that script.
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
From man source:
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.
Exporting variables only makes them available to children of the shell you export them from. There is no way of changing the environment variables in the parent shell, as you seem to be trying to do. You can change the variables in the same shell by sourcing the script using the "dot" command:
. myscript