0

I am new to bash scripting and I have encountered a problem that I quite don't understand how to slove it. These are my codes:

n=$1
e=$2
echo "$n"
if [$e -eq 1]
then
    echo "$n done"
    echo "$e"
else
    echo "$n done only"
fi

If I run the script as ./programName hello 1 The problem occurs as

./programName: line 6: [1: command not found

Only the else part runs after the first echo has been executed. I have tried starting the script in these ways:

  • ./programName hello 1

  • ./programName hello -e 1

  • ./programName -n hello -e 1

While the first and second gives the same error as above. The third doesn't execute the first echo and gives the echo of the else part as -e done only

dannyBoy
  • 37
  • 11
  • Please take a look: http://www.shellcheck.net/ – Cyrus Oct 02 '16 at 07:08
  • You need to use space after [ and before ] like this: if [ $e -eq 1 ] Plus, use double quotes around $e like this "$e" – Jaur Oct 02 '16 at 09:42
  • btw: `[` is a synonym for the "test" builtin command. [ is actually a command. So, that's why you need to use space after `[` – Jaur Oct 02 '16 at 09:57

0 Answers0