0

I'm trying to follow these explanations on if condition: http://linuxconfig.org/bash-printf-syntax-basics-with-examples

My code is

#!/bin/bash

result="test"
if [$result = "test"];
then printf "ok"
fi

But I have the following error: line 4: [test: command not found

amdixon
  • 3,753
  • 8
  • 24
  • 33
user3636476
  • 1,209
  • 2
  • 11
  • 21

2 Answers2

1

[ is a command. There must be spaces between not only it and its first argument, but between every argument.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
1

In bash (and also POSIX shells), [ is equivalent to test command, except that it requires ] as the last argument. You need to separate command and its arguments for the shell doing token recognition correctly.

In your case, bash think [test is a command instead of command [ with argument test. You need:

#!/bin/bash

result="test"
if [ "$result" = "test" ]; then
  printf "ok"
fi

(Note that you need to quote variables to prevent split+glob operators on them)

cuonglm
  • 2,561
  • 1
  • 20
  • 32