How to put value of echo pipe netcat commands into variable

9,438

Use backticks. i.e.:

var=`echo -ne "/dev/shm/test.sh" | netcat 89.196.167.2 4567`
Share:
9,438

Related videos on Youtube

Andreea
Author by

Andreea

Updated on September 18, 2022

Comments

  • Andreea
    Andreea over 1 year

    I have this command succession:

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

    and let's say it return a string like, for example "Hello...bla". On the 89.196.167.2 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 89.196.167.2 4567;echo "$var"
    

    it doesn't work. Bash returns this:

    -bash: -ne: command not found
    

    Can you please help me with a solution?

  • Dennis Kaarsemaker
    Dennis Kaarsemaker about 11 years
    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 89.196.167.2 4567)
  • Andreea
    Andreea about 11 years
    @DennisKaarsemaker Ok! I'll do that! Thank you!
  • Stéphane Chazelas
    Stéphane Chazelas about 11 years
    @DennisKaarsemaker, though backticks are obsolete and have all sort of issues, they do nest.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 11 years
    @StephaneChazelas But they only nest in aspirin bottles.