HelloWorld
HelloWorld

Reputation: 297

How to use if statment in UNIX

I was trying to execute the following commands.

de="hello world"
if [ $de -eq "hi" ]; then
....
....

because of the space between hello and the world, it out put an error. but if I define de="helloworld" it works fine. can you please tell me if there is a way I can use if statement with sentences that have spaces in it?

Upvotes: 1

Views: 1249

Answers (1)

nos
nos

Reputation: 229108

Quote the variable name,

de="hello world"
if [ "$de" = "hi" ]; then

-eq is for comparing numbers, so use = for text. See here for a nice overview on how to do various comparisions in bash.

Upvotes: 5

Related Questions