Saturday, April 23, 2022

[SOLVED] Getting "command not found" error while comparing two strings in Bash

Issue

My whole script is currently this:

#!/bin/sh   
clear;   
blanko="";   
# Dummy-Variablen
variable=Testvariable;   
if [[$variable == $blanko]];
then   
  echo "Nichts da!"   
else   
  echo $variable   
fi

and if I enter

TestSelect.sh

I get

/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found   
Testvariable

How can I fix this?


Solution

This is problem:

if [[$variable == $blanko]];

Spaces are required inside square brackets, use it like this:

[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"


Answered By - anubhava
Answer Checked By - Timothy Miller (WPSolving Admin)