Cory Klein
Cory Klein

Reputation: 55690

How do you compare a string containing an angle bracket '<' in bash?

The following bash script is giving me problems:

#!/bin/bash

if [[ $VAR -eq "<EMPTY>" ]]; then echo "Hello World!"; fi

Bash fails, complaining:

line 3: [[: <EMPTY>: syntax error: operand expected (error token is "<EMPTY>")

How can I test if the string contained in VAR is equivalent to the string "<EMPTY>"?

Upvotes: 0

Views: 388

Answers (2)

choroba
choroba

Reputation: 241918

Inside [[ ... ]], -eq has a different meaning: it is used to compare integers. You can use (( ... )) to compare integeres with normal operators, though. Use the following for strings:

[[ $VAR == "<EMPTY>" ]]

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168646

You are using the wrong operator. == is for strings, -eq is for numbers.

#!/bin/bash

if [[ $VAR == "<EMPTY>" ]]; then echo "Hello World!"; fi

Upvotes: 4

Related Questions