Neon Flash
Neon Flash

Reputation: 3233

bash unexpected token then error

I have written a bash script and I am receiving an error when I am testing a condition whether a variable is empty or not.

Below is an example script:

I have not mentioned the commands that are executed to assign values to variables a and fne but

#! /bin/bash

for f in /path/*
do
    a=`some command output`
    fne=`this command operates on f`
    if[ -z "$a" ]
    then
        echo "nothing found"
    else
        echo "$fne" "$a"
    fi
done

error: syntax error near unexpected token, "then".

I tried another variation like this:

#! /bin/bash

for f in /path/*
do
    a=`some command output`
    fne=`this command operates on f`
    if[ -z "$a" ]; then
        echo "nothing found"
    else
        echo "$fne" "$a"
    fi
done

again same error.

when I try comparing this way:

if[ "$a" == "" ]; then

again same error.

I am not sure what is the reason for the error. The value of variable a is like this:

Something with it (1) : [x, y]

it contains, spaces, brackets, comma, colon. I am enclosing the variable name in double quotes in comparison.

Upvotes: 2

Views: 5752

Answers (1)

guido
guido

Reputation: 19194

You are missing the space after the if:

#! /bin/bash

for f in /path/*
do
    a=`some command output`
    fne=`this command operates on f`
    if [ -z "$a" ]; then
        echo "nothing found"
    else
        echo "$fne" "$a"
    fi
done

Side note: if you were using vi for editing, it would have syntax-colored your typo...

Upvotes: 8

Related Questions