1

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?

0

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Removing space after '=' and adding $HADOOP_HOME solved the issue for me.
1

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")

1 Comment

Thank you. I accepted John answer because removing spaces has solved my problem completely. Thank you for your response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.