String comparison in bash is not working

后端 未结 2 1407
感动是毒
感动是毒 2020-12-18 02:29

Hi I\'m new to bash scripting. Just wrote this simple program but it is throwing error.

#!/bin/bash
os=`uname -o`
echo $os
if [\"$os\"==\"GNU/Linux\"] ; then         


        
2条回答
  •  渐次进展
    2020-12-18 03:06

    Use = for string comparison. See: http://tldp.org/LDP/abs/html/comparison-ops.html

    Also, there should be a space around the square brackets and the comparison operator, i.e.

    if [ "$os" = "GNU/Linux" ]; then 
      ^ ^     ^ ^           ^  
      | |     | |           |
       \-\-----\-\-----------\-- (need spaces here)
    

提交回复
热议问题