Freya Ren
Freya Ren

Reputation: 2164

how to write commands into shell script?

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.

Upvotes: 0

Views: 593

Answers (3)

johnsyweb
johnsyweb

Reputation: 141998

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?

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 755054

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.

Upvotes: 4

Juan Guzmán
Juan Guzmán

Reputation: 19

Have you tried setting permission to execute the file??

chmod +x filename

Upvotes: -1

Related Questions