QuestionQ11

VerifiedThe Power of the Command Line

The file script.sh in the current directory has the following contents:

#!/bin/bash  
echo $MYVAR  

The following commands are used to run this script:

MYVAR=value -  
./script.sh  

The result is an empty line rather than the value of the MYVAR variable. How must MYVAR be set so that script.sh displays its value?

Explanation

A variable assignment in the invoking shell is not inherited by a separately executed script unless it is exported. export MYVAR=value places MYVAR in the environment passed to script.sh, so echo $MYVAR prints its value.

Community Discussion

No comments yet. Be the first to start the discussion!