0

I am trying to automate the installation of nginx on multiple servers, and I have a shell script. It runs a version check if nginx is already installed and its version.

Trying to assign TMP=$(nginx -v), and instead of storing it in the variable it prints the results to console. printf "$TMP" prints an empty string

Anton Stafeyev
  • 761
  • 1
  • 7
  • 20

1 Answers1

2

The problem is that your command does not print to STDOUT but to STDERR.

Using:

TMP=$(nginx -v 2>&1)

will solve your issue, see here for more details.

norok2
  • 25,683
  • 4
  • 73
  • 99
  • it actually outputs to STDOUT, nginx is installed so i assume there should be no error – Anton Stafeyev Jul 15 '19 at 12:59
  • I do not know which version do you have, but for me (`nginx version: nginx/1.14.0 (Ubuntu)`) it is outputing to STDERR. Does the above not work for you? – norok2 Jul 15 '19 at 13:01
  • oh, wow how to check if ti outputs to stderr for a future reference ? – Anton Stafeyev Jul 15 '19 at 13:02
  • You typically cannot tell by looking at the screen. A common way is to try to redirect STDERR and/or STDOUT to different files. But my favorite is to use: https://github.com/sickill/stderred and have STDERR colored in red :-) – norok2 Jul 15 '19 at 13:06