Pistachios
Pistachios

Reputation: 77

If command with user input OS X terminal

So I'm new to the OS X terminal and I'm trying to figure out how to use the if command with the read command.

Like this:

echo stuff:
read f
if [ "f" == "y"]
then 
echo wassup
else exit

What am I doing wrong?

Upvotes: 6

Views: 23566

Answers (1)

icktoofay
icktoofay

Reputation: 129001

You're asking bash to compare whether the strings f and y are equivalent. Clearly, they're not. You need to use a variable substitution:

if [ "$f" == "y" ]

With this, it's asking “is the string consisting of the contents of the variable f equivalent to the string y?”, which is probably what you were trying to do.

You're also missing an fi (if backwards), which ends the if statement. Together:

if [ "$f" == "y" ]
then
    # true branch
else
    # false branch
fi

Upvotes: 14

Related Questions