Linux Essentials (010-160) — Question 32

The file script.sh in the current directory contains the following content:
#!/bin/bash
echo $MYVAR
The following commands are used to execute this script:

MYVAR=value -
./script.sh
The result is an empty line instead of the content of the variable MYVAR. How should MYVAR be set in order to make script.sh display the content of MYVAR?

Answer options

Correct answer: E

Explanation

The correct answer is E because using 'export MYVAR=value' makes the variable MYVAR available to the script environment. The other options do not properly set the variable in a way that the script can access it, especially since the script is executed in a subshell where non-exported variables are not inherited.