After if you need a shell command, as elsewhere. $X = $Y parsed as a shell command, that is, $X interpreted as the command name (assuming the value of the variable is one word).
You can use the command [ (also available as test ) or the special syntax [[ … ]] to compare two variables. Note that you will need spaces inside the brackets: the brackets are a separate token in the shell syntax.
if [ "$X" = "$Y" ]; then …
or
if [[ "$X" = "$Y" ]]; then …
[ … ] works in any shell, [[ … ]] only in ksh, bash and zsh.
Note that you need double quotes around variables¹. If you leave the quotation marks around, the variable will be split into several words, and each word will be interpreted as a template template. This does not happen inside [[ … ]] , but the right side = interpreted as a template template. Always put double quotation marks around substitution variables (unless you want the variable value to be used as a list of file name match patterns, not as a string).
¹ Except for the $X syntax [[ … ]] .
source share