String comparison in bash is not working

后端 未结 2 1390
感动是毒
感动是毒 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)
    
    0 讨论(0)
  • 2020-12-18 03:11

    try

    if [ "$os" = "GNU/Linux" ]
    

    note the spaces, and the single =.

    [ is actually a program, and the rest are arguments!

    0 讨论(0)
提交回复
热议问题