0

I want to write a shell script to execute commands like "export JAVA_HOME=....." How could I write a script?

I try:

#!/bin/sh
echo "test"

export PATH=$JAVA_HOME/bin:$PATH
export AWS_AUTO_SCALING_HOME=/usr/local/CLI
export PATH=$PATH:$AWS_AUTO_SCALING_HOME/bin
export AWS_CREDENTIAL_FILE=/usr/local/CLI/credential-file-path.template

But the commands are not executed.

1
  • 1
    Do you want to run them on startup ? If so, you need to add it your startup script such as $HOME/.bashrc assuming you use a bash shell. Commented Mar 4, 2013 at 3:28

3 Answers 3

4

But the commands are not executed.

They are executed, but in a sub-shell. The parent shell does not inherit these values.

Instead of executing your script, source it:

source /path/to/myscript.sh

Or

. /path/to/myscript.sh

Further reading: What is the difference between executing a bash script and sourcing a bash script?

Sign up to request clarification or add additional context in comments.

Comments

4

How are you executing your script? If you use:

$ script.sh

the environment is set for the duration of the script, but the parent shell is completely unaffected by this (Unix is not DOS!).

To get the results of the commands into your shell, use:

$ . script.sh

or in Bash you can use:

$ source script.sh

(This is a synonym for the . (dot) command, which has been in shells since the Bourne shell. The source command was in C shell first, then added to Bash.)

These read the script into the current process. Any environment variable settings affect the current process. Your profile is effectively read using . $HOME/.profile, for example.

Note that the file for the dotted command is searched for in the directories on $PATH, but the file only needs to be readable, not executable too.

Comments

-1

Have you tried setting permission to execute the file??

chmod +x filename

2 Comments

Making the script executable will not help, as detailed in the other answers.
This script doesn't need to be "run", it must be sourced.

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.