'I am trying to create user defined function for string comaparision which takes input from user in shell script

`read -p "Enter first string: " VAR1`
`read  -p "Enter second string: " VAR2`

`echo "You entered $VAR1"`
`echo "You entered $VAR2"`

`function display()`
`{`
    `a=$1`
    `b=$2`

    if [ '$a' == '$b' ]; then
       echo "Strings are equal"
    else
       echo "Strings are not equal"
    fi

`}`

`display VAR1 VAR2`

Function is not giving desired output, it always returns "Strings are not equal"



Solution 1:[1]

  • the [ takes = sign as comparison operator, not the ==. Please use =.
  • the quoting '' does not allow the shell to resolve the $, please use "" quotes instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 torbatamas