-3

I was testing a Bash script that accepts random arguments and noticed that it would say something like

./Var: line 19: [three: command not found

Here is a minimal working example:

#!/bin/bash
$1 $2 $3

echo "The first argument does $1"
if [$1 >= 2]; then
    echo "$1 has 2 words"
else
    echo "$1 has unknown amount of words"
fi
#^First
echo "The second argument does $2"
if [$2 >= 2]; then
    echo "$2 has 2 words"
else
    echo "$2 has unknown amount of words"
fi
#^Second
echo "The third argument does $3"
if [$3 >= 2]; then
    echo "$3 has 2 words"
fi
#^Third

But it would continue to run the script, is there a way i can have it run without the "command not found" appearing? Or is this just an issue within my code that is making it do this?

1 Answers1

1

The command you want to use is [ (or test), not [whatever, so it should be (for example)

[ "$3" -ge 2 ]

instead of [$3 >= 2], which generates [three: command not found if $3 is three. Additionally:

  • quote your variables;
  • [ doesn't understand >=, it understands -ge and other options;
  • >= is a redirection anyway and it didn't get to the command, regardless if the command was [ or [three. You now have a file named =.

In Bash [ is a builtin. See help [, help test.