Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 205
EpsilonAlpha Avatar asked Nov 01 '13 18:11

EpsilonAlpha


People also ask

How do I compare two strings in bash?

When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.

What is == in bash script?

You can check the equality and inequality of two strings in bash by using if statement. “==” is used to check equality and “!= ” is used to check inequality of the strings. You can partially compare the values of two strings also in bash.

What is $1 $2 in bash script?

$1 is the first argument (filename1) $2 is the second argument (dir1)

What does || in bash do?

Logical OR Operator ( || ) in Bash It is usually used with boolean values and returns a boolean value. It returns true if at least one of the operands is true. Returns false if all values are false.


1 Answers

This is problem:

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

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

[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable" 
like image 99
anubhava Avatar answered Sep 16 '22 23:09

anubhava