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,703
  • 8
  • 23
  • 33
user3636476
  • 1,069
  • 2
  • 9
  • 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
  • 699,552
  • 132
  • 1,235
  • 1,283
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,393
  • 1
  • 16
  • 30