LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-12-2011, 09:18 AM   #1
mei0fei
Member
 
Registered: Jul 2011
Posts: 38

Rep: Reputation: Disabled
[] vs [[]]


HI, all!

admin > gender="female"
admin > if [[ "$gender" == f* ]]
or
admin > if [ "$gender" == f* ]

what is the difference between "[]" and "[[]]"? I hope some simple examples can show the difference.
 
Old 07-12-2011, 09:36 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
well the difference is that [ is generally a symlink to the "test" command, whilst [[ is a shell builtin within bash. more here http://www.ibm.com/developerworks/li...est/index.html
 
Old 07-12-2011, 10:24 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
"[[]]" is usually better since you don't need to quote varaibles and operators inside it, and it supports reqular expressions. The only downside is that is doesn't work in all sh-compatible shells.
 
Old 07-12-2011, 07:53 PM   #4
mei0fei
Member
 
Registered: Jul 2011
Posts: 38

Original Poster
Rep: Reputation: Disabled
o , I see, thank you!
 
Old 07-12-2011, 08:10 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Another link for comparison:

http://mywiki.wooledge.org/BashFAQ/031
 
Old 07-12-2011, 09:40 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
See also the Bash Advanced user guide http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
  


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



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

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