Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to store the output from an netcat function into a variable. I tried a lot of different ways, but it doesn't work to me. Can someone help me? The whole scripting thing is whole new for me!

#! /bin/sh

while true;do
        var = "$(echo "RDTEMP1" | netcat -q2 sanderpi 5033)"
        echo &(var)
        echo "$(date +%Y-%m-%d%t%H:%M:%S)"
done
share|improve this question
up vote 5 down vote accepted

In shell, setting variables would be done with:

var1=toto
var2="$(echo toto | othercommand)"

You can't have spaces between your variable name, the equal character and the value you're assigning your variable with.

Then, to echo a variable, you would do:

echo $var
echo "$var"
echo "${var}"

The & character, in bash/sh, is used for "job control", which is yet another topic, ...

Start by using the following instead, tell us how it goes:

#! /bin/sh

while true;do
    var="$(echo "RDTEMP1" | netcat -q2 sanderpi 5033)"
    echo "$var"
    echo "$(date +%Y-%m-%d%t%H:%M:%S)"
done
share|improve this answer

Variable assignments in shells require no spaces around the assigner (i.e. =).

Correct:

var=$(frobnify xyz)

Wrong:

var = $(frobnify xyz)
share|improve this answer

Your Answer

 
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.