I reviewed the multiple threads on this and am still having issues, here is the command I'm trying to execute. Commands without the $() print the desired output to the console without issue, I just can't seem to put that value into a variable to be used later on.

MODEL3= $(/usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}')

but

MODEL3= /usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}'  

-will output to the console. Thanks so much!

share|improve this question
2  
the problem is the space after MODEL3=. this shouldn't be. – Theodros Zelleke Aug 2 at 15:01
4  
Grep is not needed. MODEL3=$(/usr/sbin/getSystemId | awk '/Product Name/ {print $4}') – jordanm Aug 2 at 15:06
feedback

2 Answers

That is correct:

MODEL3=$(/usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}')

But you can write the same without grep:

MODEL3=$(/usr/sbin/getSystemId | awk '/Product Name/{print $4}')

Now you have the result in the MODEL3 variable and you can use it further as $MODEL3:

echo "$MODEL3"
share|improve this answer
thank you! I simply retyped my echo statement on a new line of vi and it worked as it should. – user1571751 Aug 2 at 15:13
I am glad to help you! – Igor Chubin Aug 2 at 15:23
You should use echo "$MODEL3" with double quotes. The situations where unquoted variable interpolations are strictly correct are rare, although too many people are getting away with it too often to really understand the magnitude of the problem. – tripleee Aug 2 at 15:36
@triplee: Thank you for the tip, fixed! – Igor Chubin Aug 2 at 15:42
feedback

Spaces Not Legal in Variable Assignments

Variable assignments must not have spaces between the variable name, the assignment operator, and the value. Your current line says:

MODEL3= $(/usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}')

This actually means "run the following expression with an empty environment variable, where MODEL3 is set but empty."

What you want is an actual assignment:

MODEL3=$(/usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}')
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.