LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: 6
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 ]

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

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

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

Rep: Reputation: 72
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"
 
  


Reply


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


All times are GMT -5. The time now is 02:11 PM.

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