0

Which is the righy way to put a variable inside a command in bash?

I'm trying with:

PORT=80
`nc -zv -w30 127.0.0.1 $PORT >> /dev/null`

but it doesn'work.

1
  • maybe don't send the output to /dev/null? Commented Oct 31, 2011 at 22:00

2 Answers 2

2
  1. You don't need to use backticks if you're not capturing the output of the command. Just run the command.
  2. If you're putting the output to devnull, you don't need to append (>>), just write (>).
  3. That should work. If it's not working, something else is wrong.

    PORT=80
    nc -zv -w30 127.0.0.1 $PORT > /dev/null
    
Sign up to request clarification or add additional context in comments.

2 Comments

It works, but it still shows the output (> /dev/null not working)
Then either remove the -v option or redirect stderr like this: >/dev/null 2>&1 .
0

I assume you mean you want the output of the command stored in a variable. If so then you should first of all assign the command to a variable, and secondly don't send the output to /dev/null.

x=`nc -zv -w30 127.0.0.1 $PORT`

OR alternate syntax:

x=$(nc -zv -w30 127.0.0.1 $PORT)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.