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,859
  • 8
  • 36
  • 60
David C. Rankin
  • 75,900
  • 6
  • 54
  • 79
0

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

if [ "$option" = "1" ]; then
    echo "Blah"
fi
DanielGibbs
  • 9,503
  • 9
  • 69
  • 115
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
  • 330,190
  • 66
  • 504
  • 555
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
  • 69,051
  • 12
  • 96
  • 131