2

I have the following file - test.sh - in .:

#!/bin/sh
export ASDF=test

I do chmod +x test.sh,then ./test.sh and finally echo $ASDF and... nothing. It's as though $ASDF hasn't been set. But if I do it via the CLI instead of a shell script it works just fine and $ASDF is defined.

Why isn't the shell script working?

1

1 Answer 1

9

It is because:

./test.sh

will create a sub shell and set env variables in the sub shell. Once sub shell exits this variable isn't available in parent shell.

Use this form to avoid forking a sub shell and execute test.sh in the current shell itself:

. ./test.sh

OR:

source ./test.sh

Now that variable ASDF will be available in current shell also.

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

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.