Mark
Mark

Reputation: 70030

Bash - How to put variable in command?

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.

Upvotes: 0

Views: 421

Answers (2)

Kevin
Kevin

Reputation: 25269

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)

Upvotes: 0

mwp
mwp

Reputation: 8467

  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
    

Upvotes: 2

Related Questions