This question already has an answer here:

I have this command succession:

echo -ne "/dev/shm/test.sh" | netcat 4567

and let's say it return a string like, for example "Hello...bla". On the system, I have made a server that takes ssh commands, executes them, and returns the result to the client. That ssh program is running OK; it returns what I need, so that is not the problem.

I want to put this returned value, "Hello...bla", into a variable and use it. If I try this:

var=echo -ne "/dev/shm/test.sh" | netcat 4567;echo "$var"

it doesn't work. Bash returns this:

-bash: -ne: command not found

Can you please help me with a solution?

marked as duplicate by Gilles, jasonwryan, daisy, vonbrand, Renan Mar 29 '13 at 1:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Use backticks. i.e.:

var=`echo -ne "/dev/shm/test.sh" | netcat 4567`
  • 7
    Don't use backticks. They don't nest and are not always easy to distinguish from single quotes. It's better to use $(...) instead: var=$(echo -ne "/dev/shm/test.sh" | netcat 4567) – Dennis Kaarsemaker Mar 28 '13 at 9:28
  • @DennisKaarsemaker Ok! I'll do that! Thank you! – Andreea Mar 28 '13 at 9:33
  • 3
    @DennisKaarsemaker, though backticks are obsolete and have all sort of issues, they do nest. – Stéphane Chazelas Mar 28 '13 at 10:12
  • 1
    @StephaneChazelas But they only nest in aspirin bottles. – Gilles Mar 28 '13 at 23:29

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