1

I'm doing something wrong on the string comparison below. It works if I set a variable and compare that, but I can't copy the value to a string. Does anyone know what's wrong?

$ if [ "$(lsb_release -i)" = "Distributor ID: RedHatEnterpriseClient" ]; then echo yes; else echo no; fi
no
$ lsb_release -i
Distributor ID: RedHatEnterpriseClient
$ var="$(lsb_release -i)"
$ if [ "$(lsb_release -i)" = "$var" ]; then echo yes; else echo no; fi
yes
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Aido
  • 166
  • 14

3 Answers3

3

That's because the output from lsb_release -i uses a tab instead of space:

lsb_release -i|od -c
0000000   D   i   s   t   r   i   b   u   t   o   r       I   D   :  \t
0000020   R   e   d   H   a   t   E   n   t   e   r   p   r   i   s   e
0000040   S   e   r   v   e   r  \n

Notice the \t at the end of the first line. Include that in your comparison string, and it will succeed:

if [ "$(lsb_release -i)" = $'Distributor ID:\tRedHatEnterpriseServer' ]; ...
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
3

In this case I suggest -is:

if [ $(lsb_release -is) = "Debian" ]; then echo yes; else echo no; fi
JJoao
  • 11,887
  • 1
  • 22
  • 44
0

It doesn't seem like a good solution, but this works:

if [ "$(echo $(lsb_release -i))" = "Distributor ID: RedHatEnterpriseClient" ]; then echo yes; else echo no; fi
Aido
  • 166
  • 14