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,814
  • 8
  • 25
  • 34
user3636476
  • 1,357
  • 2
  • 11
  • 22

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
  • 776,304
  • 153
  • 1,341
  • 1,358
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,766
  • 1
  • 22
  • 33