4

I am running a script which is echoing the environment variables in .profile file and than I am running the script, but I am getting following error

I tried following:

node@node-virtual-machine:~$ cat env.sh 
#!/bin/bash

echo 'export JAVA_HOME=/home/node/jdk1.6.0_45' >> /home/node/.profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /home/node/.profile
cd /home/node/
source .profile
node@node-virtual-machine:~$ sh env.sh 
sudo: source: command not found

How to execute .profile within a script?

3 Answers 3

3

Instead of:

sh env.sh

You should run:

bash ./env.sh

Besides instead of:

source .profile

use:

source ~/.profile
Sign up to request clarification or add additional context in comments.

4 Comments

Your solution does not gives me error, but the changes are not getting reflected after running the script
environment variables are not getting set :(
But your shown script isn't setting/printing any env variables? If you print value right after source ~/.profile you will see all the values from ~/.profile
@Prats: can you please show how source ~/.profile worked in scripts itself? I am in a similar situation and stuck!!
0

If .profile exists as /home/node/.profile, you have all that is necessary.

Comments

0

Your script says /bin/bash at the top but you run it with sh which is probably dash on your system. You probably are already running Bash at the prompt, so you should say source env.sh instead of sh env.sh if you want the variables to be exposed to your terminal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.