Is there any difference in bash shell in [ .. ] and [[ .. ]] ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there any difference in bash shell in [ .. ] and [[ .. ]] ?
# 1  
Old 07-02-2008
Is there any difference in bash shell in [ .. ] and [[ .. ]] ?

Could someone tell me if there is any difference in using square brackets single or doubled pair?

So, do those two some how could be treted by shell differently:
Code:
if [ <condition> ] ; then <statements;> else <statements;> fi

and
Code:
if [[ <condition> ]] ; then <statements;> else <statements;> fi

Maybe any one could provide with any additional shell usage compare to another?

Thank you,
Alex
# 2  
Old 07-02-2008
If you use "[.....]" you are actually using /bin/test (/bin/[). Try man [ and you will get the test(1) manpage.

If you use [[.....]] you are using bash's builtin test . Using a builtin is faster and more efficient since no fork/exec is required.
# 3  
Old 07-02-2008
These days they are both built-ins:

Code:
$ echo $BASH_VERSION
3.2.39(19)-release
$ type [
[ is a shell builtin
$ type [[
[[ is a shell keyword
$

[ is included mostly for backward compatibility to Bourne shell and the /usr/bin/test that fpmurphy mentioned. I usually prefer the syntax of [[.
# 4  
Old 07-03-2008
Thank you, guys for reply!
So, from user point of view there is no difference in usage [ ... ] or [[ ... ]]?
Is it correct?

Having by today both as a part of shell (buildin and keyword) now it is really hard to say witch one could be more efficient, right?
# 5  
Old 07-03-2008
# 6  
Old 07-03-2008
For portability use [.....] . For efficiency, use [[.....]]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. UNIX for Beginners Questions & Answers

Understanding the difference between individual BASH login scripts

Hello... and thanks in advance for reading this or offering me any assistance I'm trying to understand specific differences between the various login scripts... I understand the differences between interactive vs non-interactive and login vs non-login shells... and that's not where my question... (4 Replies)
Discussion started by: bodisha
4 Replies

4. UNIX for Dummies Questions & Answers

Difference between inbuilt suid programs and user defined root suid programs under bash shell?

Hey guys, Suppose i run passwd via bash shell. It is a suid program, which temporarily runs as root(owner) and modifies the user entries. However, when i write a C file and give 4755 permission and root ownership to the 'a.out' file , it doesn't run as root in bash shell. I verified this by... (2 Replies)
Discussion started by: syncmaster
2 Replies

5. Shell Programming and Scripting

Difference between kshell and bash shell scripts Example cited

Hi All, I need some urgent help regarding some info. I have a cluster of servers for which I have two scripts for management. control.sh is a bash script meant for restarting/stopping the servers. manger.ksh is a kshell script. It is a master script to manage restarting/stoppping and... (3 Replies)
Discussion started by: ankur328
3 Replies

6. Shell Programming and Scripting

Help with displaying difference between two arrays (Bash)

The code I have is kind of long, so I'm just posting the part I'm struggling with. I've found many examples online of comparing two arrays in Bash and printing the difference between them. I've tried them all, even mixed and matched some examples with no luck. I know this can't be as hard as I'm... (9 Replies)
Discussion started by: Azrael
9 Replies

7. Shell Programming and Scripting

Bash script getting difference values from files

Hi I want to write a BASH script. I have files updated every hour from two platforms.The file names are : - Falcon_smsCounters_1200020708.log - Canari_smsCounters_1200020708.log 1200 refers to hour twelve and 020708 to 02 july 2008. Inside every file i have a list of parameters... (2 Replies)
Discussion started by: salimayoub
2 Replies

8. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

9. UNIX for Dummies Questions & Answers

Difference in Shell Scripts

Hi, Is anyone can help me to find out the difference in Shell Scripts between HP and Sun. Thanks in advance, Vijay R (3 Replies)
Discussion started by: rv_kumar
3 Replies

10. Answers to Frequently Asked Questions

Difference between ksh,bash and different shells.

Hello Everyone, Can someone please tell me the key difference between the different shells availabe i.e. ksh,bash,(i don't know the others ones. :confused: (5 Replies)
Discussion started by: a25khan
5 Replies
Login or Register to Ask a Question