whats the function of brackets here ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting whats the function of brackets here ?
# 8  
Old 01-09-2011
To elaborate further, while, as already mentioned, [ and test are synonyms,
the [[ version introduces some important improvements. Notice that the double bracket
syntax is not standard and is not supported by some shells.

So, some of the differences are that in [[ ... ]] context:

1. No shell word splitting and path expansion (globbing) are performed:

Code:
bash-4.1.9(1)[t]$ touch aa
bash-4.1.9(1)[t]$ x='a* b'
bash-4.1.9(1)[t]$ set -x;[ $x == 'a b' ];set +x
+ '[' aa b == 'a b' ']'
bash: [: too many arguments
+ set +x
bash-4.1.9(1)[t]$ set -x;[[ $x == 'a b' ]];set +x
+ [[ a* b == \a\ \b ]]
+ set +x

2. More readable(?) boolean operators are supported.

For example, you can use:

Code:
[[ 1 -gt 0 && 2 -gt 1 ]]

Instead of:

Code:
[ 1 -gt 0  -a  2 -gt 1 ]

3. Some shells support an additional binary operator, =~.
When it is used, the string to the right of the operator
is considered an extended regular expression:

Code:
bash-4.1.9(1)[t]$ [[ aa =~ ^a+ ]] && echo matches
matches

# 9  
Old 01-10-2011
[ is a binary

It may help your understanding further to realise that [ is actually a binary. See this dialogue from my Debian Lenny system :

Code:
mega:~# cd /usr/bin
mega:/usr/bin# ls -l '['
-rwxr-xr-x 1 root root 34504 2008-04-04 15:22 [
mega:/usr/bin# ls -l test
-rwxr-xr-x 1 root root 22852 2008-04-04 15:22 test

As an historical note, on an old SVR0 Unix box I once administered, [ and test were hard linked, i.e. they were the same binary. As you can see from the output above, this is not the case in Linux.

Regards
Neil
# 10  
Old 01-10-2011
Although most shells use regular shell-builtins to overrule the binaries and speed things up nowadays....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python - Function print vs return - whats wrong

All, I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this #!/usr/bin/python def div4and6(s,e): for i in range(s,e+1): if... (5 Replies)
Discussion started by: oky
5 Replies

2. Shell Programming and Scripting

Capture the value between brackets

Hi I am having hard time getting this right and need some help. I Have 3 files, and everyone contains the following:- File1 Today, we have Name(Jack) Age(19) Class (A2) Today, we have Name(Kim) Class (G9) File2 Today, we have Name(Lee) Age(19) Class (A2) Today, we have... (8 Replies)
Discussion started by: samsan
8 Replies

3. Shell Programming and Scripting

Function works, trigger causes syntax error, whats my fault??

Needing a hint. Creating that function called Meter my simple script works well. What I want now is to start the last four commented lines to include or trigger a reaction, if there are more than n lines in that .txt-file it shall display that message for example. But the interpreter says there is... (3 Replies)
Discussion started by: 1in10
3 Replies

4. Shell Programming and Scripting

Brackets

Hi all. i need a small help. i have written an exit code, which will check whether mo.sh is successful or not. if the status is >0 it will exit the shell. 1>Do you guys think it is a correct way to write? 2>what if i change the double bracket to single. how will it change the o/p. ... (1 Reply)
Discussion started by: sub
1 Replies

5. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

How to get the value in the first brackets

Hello, I am trying to write a script using ksh. And I want to get the value within the first brackets of a string. For example: 14/04/11 11:35: 00 This is (nn) from the earth. Then i hope to get nn in this case. Can any one advise me how to implement it? Thank you very much! nn (2 Replies)
Discussion started by: n_n
2 Replies

7. Shell Programming and Scripting

Get value between brackets

Hi I am having hard time getting this right and need some help. I Have several log files, and everyone contains the following 3 lines at the end: 4 ETW000 Disconnected from database. 4 ETW000 End of Transport (0000). 4 ETW000 date&time: 13.01.2011 - 08:03:28 I need to capture the value... (7 Replies)
Discussion started by: nimo
7 Replies

8. Shell Programming and Scripting

Whats wrong in the Function ?

Need your assistance, to find the bug in the function. Function usage erroring out even after passing parameters. usage() { if || ; then echo "************************************************************" echo " CHECK USAGE FOR CORRECT PARAMETERS ... (26 Replies)
Discussion started by: raghunsi
26 Replies

9. UNIX for Dummies Questions & Answers

usage of brackets for if

Hi, I would like to know about the usage of brackets to negate a set of boolean evaluations an equivalent to what i am trying is below if or || ] ] then echo"valid time of day" else echo "invalid input" exit fi the bracket structue as mentioned doesnt work .. i am using ksh.... (4 Replies)
Discussion started by: lakshmikanth
4 Replies

10. Shell Programming and Scripting

Whats wrong with my function?? <newbie>

First of all im using Bash, on a Debian-based machine. I tried to write a function that if the ls program found listed more than 25 lines I would automaticly use "ls | less". Its on another computer but if I recall it looked something like this... Note: some code may look strange because im on... (4 Replies)
Discussion started by: riwa
4 Replies
Login or Register to Ask a Question