010-160 Exam QuestionsBrowse all questions from this exam

010-160 Exam - Question 68


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?

Show Answer
Correct Answer: E

To ensure that the variable MYVAR is available to the script, it must be exported. The command 'export MYVAR=value' sets the MYVAR variable and exports it to the environment, making it available to any subsequently executed programs, including shell scripts. Simply setting the variable with 'MYVAR=value' without exporting it will not make it available to child processes.

Discussion

2 comments
Sign in to comment
sasquatchshrimp
Dec 13, 2020

A good source to look into more on this. https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/

Miketunzy
Dec 11, 2021

Reviewed. Thank you!

FM221228Option: E
Oct 2, 2022

We need to export the variable for it to be inherited by another program - including a shell script.