6

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?

1 Answer 1

14

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
Sign up to request clarification or add additional context in comments.

1 Comment

@SpectrumCreations Also, note that you do not have a space before the closing ] in your original post; it's required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.