0

I need to read a string variable and to check if it's equal to string 'qwe'. I try this code:

read VAR1
if ["$VAR1" == "rwx"]; then
    current=$current+1
fi

but terminal says

[rwx : command not found

Axmed
  • 41
  • 4

1 Answers1

2

[ is a command, not just syntax. Commands need to be separated from their arguments with whitespace:

if [ "$VAR1" = "rwx" ]; then ...
#   ^               ^

See help if at an interactive prompt:

$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
    Execute commands based on conditional.

    The `if COMMANDS' list is executed.  If its exit status is zero, then the
    `then COMMANDS' list is executed...

There's nothing in there about [...], what comes after if is a command list, and the exit status determines the "success" of the conditional.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Adding to this, like any other UNIX command, you can look it up with `man [`, which on my machine shows that it is an alias of the `test` command – N. Kern Apr 18 '22 at 15:21
  • 1
    Right. I recommend this sequence: `help [` then `help test` then `help [[` in bash. – glenn jackman Apr 18 '22 at 15:22
  • 1
    Going to the man page _first_ may not give you the best information: for example `test` is a shell builtin but also an external command (historical reasons). If `type $command` shows "$command is a shell builtin", read the `help` information (or the bash man page) before reaching for `man`. – glenn jackman Apr 18 '22 at 15:26