0

I don't know why this isn't working. Please help!

#!/bin/bash
clear
echo "Enter an option"
read $option
if ("$option" == 1) then
  echo "Blah"
fi

I tried like this

if ("$option" -eq 1) then

I can't see why the if statement isn't being run. All I want to do is check what the user entered and do something depending on the value entered.

4 Answers4

0

Bash if uses [ or [[ as test constructs, instead of parenthesis which are used in other languages.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

That is not the syntax for an if statement in bash. Try this:

if [ "$option" = "1" ]; then
    echo "Blah"
fi
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
0

The syntax for an equality check is:

if [[ $option == 1 ]]; then
  echo "Blah"
fi

Or, for compatibility with older non-bash shells:

if [ "$option" = 1 ]; then
  echo "Blah"
fi

In either one, the whitespace is important. Do not delete the spaces around the square brackets.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

I'm not sure where you got your syntax from...try this:

if [ "$option" -eq 1 ]; then

In the shell, [ is a command, not a syntactic construct.

Alternatively, you can use an arithmetic context in bash:

if (( option == 1 )); then
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141