How to see Shell Scripts Variables defined

2019-09-06 11:31发布

问题:

Inside my Shell Script , i have this line present

RUN_CMD="$JAVA_HOME/bin/java -cp $CLASSPATH  com/mypack/BalanceRunner 

Could anybody please tell me , what is meant by this command RUN_CMD and where can i see this RUN_CMD defined

Thanks for reading

回答1:

That's defining an environment variable RUN_CMD (looks like a quote omitted at the end, though). It's shorthand for running that Java command (which defines where to find some classes and then specifies which class to run - BalanceRunner)

The variable is in scope for the current process (a shell, or shell script, most likely). You can see what it's set to by doing:

echo $RUN_CMD

(note: specifics are shell-dependent but the above is true for Bourne shell derivatives certainly)



回答2:

The line you quote is the assignment.

As Brian said, it's only in scope for the current process.

When you run a script (say with ./<script> or bash <script>), a new shell is created in which the script is executed. As such, once the script finishes and you're returned to the prompt, all the variables assigned in the script will be undefined.

The exception to this is of course if you execute the script in the current shell, say by executing it with . <script> or source <script> (see your shell's manual for more details).

You can experiment with this on the prompt:

$ FOO=bar
$ if [ $FOO ]; then echo "FOO defined"; else echo "FOO undefined"; fi
FOO defined
$ echo $FOO
bar
$ unset FOO
$if [ $FOO ]; then echo "FOO defined"; else echo "FOO undefined"; fi
FOO undefined
$ echo $FOO

$ echo 'FOO=bar' > temp
$ . temp
$ echo $FOO
bar
$ unset FOO
$ bash temp
$ echo $FOO

$        

As for the actual content and purpose of the variable in your script, I think Brian and others answer this excellently.



回答3:

You set the value of an environment variable. As to the title of your question:

 echo $ENV_VAR_NAME

will print the value of a defined environment variable to the console



标签: linux shell