Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-02-2010, 05:04 PM   #1
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Bash: if statement syntax

A simple test which doesn't work.
if [$x==10]
then echo hey
The error message.
bash: [10==10]: command not found
Bash version.
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 <>

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 ]

Last edited by ghantauke; 12-02-2010 at 05:09 PM.
Old 12-02-2010, 06:30 PM   #2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Those spaces are important
Old 12-02-2010, 07:41 PM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
Or when comparing numbers, use the construct designed to do so:
if (( x == 10 ))
Old 12-03-2010, 10:04 AM   #4
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
if [ $x == 10 ]
echo hey
please note the spaces, "[" is also a command so you need to separate it from "if".
and run the script using
bash "scriptname"


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
gfortran: Syntax error in DATA statement at (1) science_guy Programming 1 10-16-2009 01:10 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
[Python] Invalid Syntax - If statement LinuxCrayon Programming 4 12-07-2007 10:11 AM
htdig.conf start url: statement syntax for local indexing. AwesomeMachine Linux - Software 0 05-02-2006 03:02 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration