0

Hello im new to bash scripting, i have a simple program which is not working for me. I assume its a syntactical error.

#!/bin/bash
#example1.sh
read Age
if ["$Age" -lt "18"]; then
    echo "You must go to school"
fi

When i input a 1 it says [1: command not found

2 Answers 2

6

You need spaces:

if [ "$Age" -lt "18" ]; then

(Summary: Bash syntax rules are appalling.)

Sign up to request clarification or add additional context in comments.

5 Comments

if test "$Age" -lt 18; then is a much more reasonable syntax.
if ((Age < 18)); then is an even more reasonable syntax - the spaces are optional.
[ is not part of the syntax of any Bourne shell. (( is part of the syntax for bash, but is not part of the syntax of many other shells.
As William Pursell states, [ is not part of the syntax (nor is [[) they are commands. Commands must be surrounded by whitespace (or be preceded or followed by certain other characters such as ; or &, etc.).
@DennisWilliamson: Granted. But the fact that brackets are commands and not just part of the language grammar is what's appalling!
0
#!/bin/bash
#example1.bash

read Age
if(($Age < 18)); then
echo "You must go to school"
fi

This code works when it is run in bash. Bash and sh are not completely the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.