LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash: if statement syntax (https://www.linuxquestions.org/questions/linux-newbie-8/bash-if-statement-syntax-848008/)

ghantauke 12-02-2010 04:04 PM

Bash: if statement syntax
 
A simple test which doesn't work.
Code:

x=10
if [$x==10]
then echo hey
fi

The error message.
Code:

bash: [10==10]: command not found
Bash version.
Code:

bash-4.1$ bash --version
GNU bash, version 4.1.7(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Checked for syntax online which seems to suggest that I'm using the correct syntax. Bash syntax is annoying :( Help!


Nevermind found the solution myself.
if [$x==10] should be rewritten as if [ $x -eq 10 ]

AlucardZero 12-02-2010 05:30 PM

Those spaces are important

grail 12-02-2010 06:41 PM

Or when comparing numbers, use the construct designed to do so:
Code:

if (( x == 10 ))

bsat 12-03-2010 09:04 AM

Code:

x=10
if [ $x == 10 ]
then
echo hey
fi

please note the spaces, "[" is also a command so you need to separate it from "if".
and run the script using
bash "scriptname"


All times are GMT -5. The time now is 09:23 PM.