1

I am trying to compare a substring in bash (How to check if a string contains a substring in Bash). Normally I can do this like so:

if [[ "sdfdsfOpenSSHdsfsdf" == *"OpenSSH"* ]]; then echo "substring found"; fi

However, when using an interactive command it doesn't work:

if [[ $(ssh -V) == *"OpenSSH"* ]]; then echo "substring found"; fi

The output of ssh -V is OpenSSH_8.4p1, OpenSSL 1.1.1h 22 Sep 2020, so I would expect the substring to be matched. What am I missing?

Kyu96
  • 1,159
  • 2
  • 17
  • 35

1 Answers1

2

the output of ssh -V goes to stderr, you need to redirect to stdout in order to capture it:

ssh -V 2>&1

so the following works:

if [[ $(ssh -V 2>&1) == *"OpenSSH"* ]]; then echo "substring found"; fi
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Ah, that makes perfect sense. Didn't know that the version output goes to stderr. Thanks! – Kyu96 Dec 02 '20 at 08:35