0

I have the following test.sh script:

#!/bin/sh
echo "MY_VARIABLE=$MY_VARIABLE"

Well, if I execute the following:

export MY_VARIABLE=SOMEVALUE
/bin/bash test.sh

it prints:

MY_VARIABLE=

Why the MY_VARIABLE is not read in the test.sh script?

You can reproduce the context here using the following script:

touch test.sh
chmod a+x test.sh
echo "#!/bin/sh" >> test.sh
echo "echo "MY_VARIABLE=$MY_VARIABLE"" >> test.sh
export MY_VARIABLE=something
/bin/bash test.sh
3
  • The script from the question below "You can reproduce the context here using the following script:" reproduces the problem ("it prints: MY_VARIABLE=") but doesn't reproduce the script shown below "I have the following test.sh script:". The script below "I have the following test.sh script:" doesn't reproduce the problem, it prints MY_VARIABLE=something as expected. Commented Jan 21, 2019 at 17:24
  • Since you say below the problem involves Docker, not the example you posted, perhaps you should update your question to accurately reflect the situation. Commented Jan 22, 2019 at 20:05
  • I have found the reason, I accidentally inserted a double CMD command in the Dockerfile, so my variable did never set and the container started without the variable. Commented Jan 22, 2019 at 20:11

2 Answers 2

3

In your script to create the context, the line

echo "echo "MY_VARIABLE=$MY_VARIABLE"" >> test.sh

creates the following line in test.sh:

echo MY_VARIABLE=

if MY_VARIABLE was unset before. The expansion of $MY_VARIABLE is done in the shell that prepares your context.

If you use single quotes

echo 'echo "MY_VARIABLE=$MY_VARIABLE"' >> test.sh

the script test.sh contains the correct line

echo "MY_VARIABLE=$MY_VARIABLE"

and prints MY_VARIABLE=something as expected.

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

7 Comments

In other words, the problem is in the script to reproduce the issue, as it's not reproducing what the question asks.
@BenjaminW. Yes and no. The script to reproduce the issue actually reproduces the issue of not showing the value of the variable but it does not reproduce the script shown at the beginning of the question (which does not reproduce the issue).
Thanks for the answer, I have tried your suggestion in the link I have shared and it works. But in a docker container it still not work, the MY_VARIABLE is always empty.
@AlessandroC What exactly does not work? Your script to reproduce the context with my modification? Your real script?
I think I have a problem with docker, not with the script.
|
0

Everything works well but if you want your parent process to keep environment update, you must source your script:

source test.sh

Otherwise, changes will only have effect during the execution of your script.

You can consider it the same as sourcing your ~/.bashrc file.

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.