title: How to display nc return value in Linux shell script?
body: <p>I am using <code>nc</code> command in my Linux box like below to check if a port is listening;</p>
  
  <p>This displays success message:</p>
  
  <pre><code>nc -z 192.168.0.2 9000
  </code></pre>
  
  <p>This displays 0:</p>
  
  <pre><code>echo $?
  </code></pre>
  
  <p>I have combined it in a shell script .sh file like below;</p>
  
  <pre><code>#!/bin/sh
  nc -z 192.168.0.2 9000
  echo $?
  </code></pre>
  
  <p>This displays <code>1</code> instead of expected <code>0</code>. Again, if I modify my script like below, it works;</p>
  
  <pre><code>#!/bin/sh
  echo nc -z 192.168.0.2 9000
  echo $?
  </code></pre>
  
  <p>But here the problem is, it displays success message on one like, then displays 0 in next line. I don't want success message, and I am expecting 0. What is wrong here and how can I fix this?</p>