0

whatever if statement I try to put into my scripts, I always get the same : syntax error near unexpected token fi'.

For example:

#!/bin/sh
echo $1
echo $2
if [ $1 -eq 0 ]
then
echo $2
fi

I've tried :

#!/bin/sh
echo $1
echo $2
if [[ $1 -eq 0 ]]
then
echo $2
fi

And:

#!/bin/sh
echo $1
echo $2
if [ $1 -eq 0 ]; then
echo $2
fi

And many other options, none seem to work.

  • 4
    Did you by any chance write these scripts on a Windows machine? Also, how do you actually run the scripts from the command line? – Kusalananda Apr 4 at 21:49
  • 2
    /bin/sh isn't always bash, if you use "bashisms" put an explicit #! /bin/bash – xenoid Apr 4 at 22:41
  • 1
    I'd recommend you double-quote your variables, too. For example, [ "$1" -eq 0 ] – roaima Apr 4 at 22:47
  • 2
    If I copy your script and put CRLF line endings in it, I am able to cause similar syntax errors, depending on where I put the CRLF. Run file against your script, and see if it says "with CRLF, LF line terminators" – Tim Kennedy Apr 4 at 22:52
  • 1
    dos2unix for-the-win – 0xSheepdog Apr 4 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.