Atom
Atom

Reputation: 788

shell script not printing value

I am a newbie in shell programming trying to run the below script. I am unsuccessful in printing the value assigned to the hadoop_home variable in the below example. Please help me in understanding my mistake.

#!/bin/sh
echo "Holaaaa"
hadoop_home= echo "$HADOOP_HOME"
java_home= echo "$JAVA_HOME"
echo "Printing variable values"
echo "${hadoop_home}"
echo "${java_home}"

On executing the above shell, it prints

Holaaaa
/usr/hdp/2.6.0.1-152/hadoop/
/usr/lib/jvm/java-1.7.0-openjdk-amd64/jre/
Printing variable values

The variable value in hadoop_home and java_home is empty. Why is it empty and how can I get the values assigned to these variables?

Upvotes: 1

Views: 3721

Answers (2)

Comforse
Comforse

Reputation: 2057

Remove the echo and the double quotes from around the variables.

The echo command just prints to the standard output, it does not assign any values to the variables in your case as you are not in the standard output. You can capture the result though with a slight change:

$(echo "$HADOOP_HOME")

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361565

Assignments must not have spaces around the =. To capture the output of a command use $(cmd).

hadoop_home=$(echo "$HADOOP_HOME")

This can be simplified to:

hadoop_home=$HADOOP_HOME

Upvotes: 3

Related Questions