2
votes

Why does this simple if statement result in a syntax error?

#!/bin/bash

if [[ 1 == 1 ]] ; then
#    echo "hello"
fi

The error is

line 5: syntax error near unexpected token `fi'

It works as expected if the echo is uncommented.

Edit

Thanks, fixed the error using :. Silly bash. =P

#!/bin/bash

if [[ 1 == 1 ]] ; then
:#    echo "hello"
fi
1

1 Answers

5
votes

Because, as you can see in man bash, the correct syntax for if is

if list; then list; [ elif list; then list; ] ... [ else list; ] fi

And if you look up the definition of list, it says

A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ││, and optionally terminated by one of ;, &, or .

This "one or more" is the reason why your example is not a valid syntax.