LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-02-2012, 03:16 PM   #1
grimx
Member
 
Registered: Oct 2008
Distribution: Slackware 14 i686
Posts: 111

Rep: Reputation: 16
bash scripting Q


Just wanted to know what the difference is between
if() and if[]
 
Old 05-02-2012, 03:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
() wraps up a sub-shell, [ ] is an alias for 'test'.
 
Old 05-02-2012, 03:19 PM   #3
grimx
Member
 
Registered: Oct 2008
Distribution: Slackware 14 i686
Posts: 111

Original Poster
Rep: Reputation: 16
So, is it best to use [] instead of (), when test for a condition??
 
Old 05-02-2012, 03:47 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
It depends on what you're testing ...
 
Old 05-02-2012, 07:49 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
You might also want to look at this page http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 05-03-2012, 07:21 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

The first link in particular will cover all the scripting basics you need to know. It's important to understand and use the exact syntax as described in the documentation, as everything has very precise meanings.

That said, there are several intricacies involved here.

Code:
foo() { text="This is a function called foo." ; echo "$text" ;}

$ foo
This is a function called foo.

$ echo foo()
bash: syntax error near unexpected token `('
When parentheses are placed immediately after a command word (i.e. only at the beginning of the line), it starts a function definition. Otherwise it just results in a syntax error.

Code:
$ ( text="This is an echo command running in a subshell." ; echo "$text" )
This is an echo command running in a subshell.

$ echo "$text"
			#outputs a blank line
When parentheses are placed around a command or set of commands, it splits off a subshell to execute the contents in. This can have many uses, such as not having them affect the main shell's environment.


Code:
x=100 y=99

if [ "$x" > "$y" ]; then echo "$x is greater than $y" ; else echo "$y is greater than $x" ; fi
if [[ $x > $y ]]; then echo "$x is greater than $y" ; else echo "$y is greater than $x" ; fi

if [ "$x" -gt "$y" ]; then echo "$x is greater than $y" ; else echo "$y is greater than $x" ; fi
if [[ $x -gt $y ]]; then echo "$x is greater than $y" ; else echo "$y is greater than $x" ; fi

if (( x > y )); then echo "$x is greater than $y" ; else echo "$y is greater than $x" ; fi
"[" is a synonym for the test command, with arguments following them. Note that spaces between the elements are important, and quotes should be used around variables. "[[" is a newer bash-specific keyword, which both replaces test and adds new features, including not needing to quote variables.

Pay attention to how the comparisons work in the above examples. Inside the square brackets, ">,<,=", etc. are considered string comparisons, and so 99 can indeed be "greater than" 100, depending on the sorting order set in your locale. To do numeric comparisons, use "-gt,-lt,-eq", etc.

Even better, use "((..))" which is a bash keyword structure for evaluating arithmetic operations (integer only).

Finally...

Code:
x=foo
if grep -q "$x" file.txt ; then echo "File contains [$x]." ; fi

if ( x=foo ; grep -q "$x" file.txt ) ; then echo "File contains [$x]." ; fi
Constructs like if aren't limited to just "[/[[/((". They can check the status of any command (grep, in this case). If the command exits successfully, the subcommands will run. Note that the only difference in the above two examples is that the second one runs in a subshell, as explained before, and so "$x" will disappear after the command is run. The exit status of a subshell is usually the status of the final command in it.


So to sum it up, when using bash (or ksh), integer comparisons should be done with "((..))" and string and file comparisons with "[[..]]". Only use "[..]" if you need posix compliance, which for the average user is rarely. And "()" itself is for other purposes and isn't directly used in tests.


Here are few more links concerning conditional expressions:
http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression

Last edited by David the H.; 05-03-2012 at 07:23 AM.
 
Old 05-03-2012, 07:08 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
Quote:
"[[" is a newer bash-specific keyword,
Actually, I first got taught about them in a ksh course
As advised on the course, I always use them in ksh (or bash), instead of [ ] or 'test'.

Last edited by chrism01; 05-03-2012 at 07:09 PM.
 
Old 05-04-2012, 11:07 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Yes, of course it's in ksh too. I believe they probably even initiated it. In fact, quite a few ksh innovations have been subsequently adopted by bash, and vice-versa. There's a lot of similarity between the two shells due to this.

When I said "bash-specific", what I really meant was that it wasn't a posix-compliant or otherwise universally-available feature, and that you can't just assume it being there if you use another shell. But since the topic question was about bash specifically, I didn't want to further clutter up my post with relatively unimportant details like that. Even so, I did mention it in passing in my final paragraph.

Thanks for allowing the clarification.
 
Old 05-07-2012, 08:07 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
No worries
Actually, the course was so long ago, Linux had barely made it out of Finland
I was working on Sequent (later IBM) https://en.wikipedia.org/wiki/Non-Uniform_Memory_Access systems.
 
  


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
Need Help with bash scripting humlhr Linux - General 8 12-14-2011 09:55 AM
bash scripting ninjafairy Programming 11 05-18-2011 10:00 AM
bash scripting - help? mickle026 Linux - General 8 08-29-2009 05:12 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
help bash scripting sh4d0w13 Linux - Newbie 5 08-15-2005 02:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 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