0

I'm trying to code a simple test for a directory, but keep running into a problem I've been unable to solve.

 prompt> if [ -d "./mydir" ] then echo "yes"; fi
 -bash: syntax error near unexpected token 'fi'

I've tried a variety of quotes, semi-colons, line-breaks, etc.
But always run into this syntax error.

Can you explain what I've done wrong here?

0

1 Answer 1

5

You need a semicolon to separate the condition command and the then keyword:

if [ -d dir ]; then echo yes; else echo no; fi
# ...........^

You can of course use a newline instead of the semicolon

Or, use a conditional operator

[ -d dir ] && echo yes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .