brackets vs parentheses - single and double


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers brackets vs parentheses - single and double
# 1  
Old 09-25-2008
brackets vs parentheses - single and double

hi, unix gurus.

i am wondering if someone can give me a clear explanation of the differneces between parentheses and brackets, both single and double.


i have heard that double parentheses (( are used for numerical expressions and that single brackets [ are used for strings. but i see very different behavior on this forum and elsewhere. i would love some clarity!


thanks for your help as usual.
# 2  
Old 09-25-2008
First read the man page for test.

[[ and [ are shell builtins (in modern shells)
for test. test or [[ - you have to complete them with ]] - are use either in if statments or in combined statements
Code:
if [[ 1 -eq $var ]] ; then
   echo "hi there"
fi
# same thing written another way as a one-liner
[[ 1 -eq $var ]] &&  echo "hi there"

# test if a file exists
[[ -f somefile ]] && echo "found somefile "

the -eq -gt -lt -ge -le comparisons are for comaring integer numbers in [[ ]]
comparing strings- [[ "$string1" = "$string2" ]]

There are other modern operators for testing file times: for example -ot -nt
See the ksh man page

$(( )) does arithmetic operations - whole numbers (long integers).
allowed operators are + - * / and some others like %.
$(( 3 + 1 )) evaluates a 4

$( some command[s] ) runs one or more commands in background
it works just like the backticks: `command`
read all of a file into an envrionment variable
myvar=$(< somefile)
put the date in a variable
myvar=$( date )
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

2. UNIX for Dummies Questions & Answers

Single or double square brackets

Hi frieds, I don't understand the difference between single square bracket and double square brackets in a IF condition. Ex. if ; then RETURNJOB=1 else RETURNJOB=0 fi It run, but this if ]; then RETURNJOB=1 else RETURNJOB=0 fi (4 Replies)
Discussion started by: dogshort
4 Replies

3. Shell Programming and Scripting

Rules with using double parentheses in Bash

Hi.could you explain me what are the rules when we are using double parentesis in if statement,if I put ,the code is working ,with (( is not #!/bin/bash if (($# > 0)) then if ((! -d "$1")) then echo "Directory $1 not found" fi else echo Problem fi (8 Replies)
Discussion started by: lio123
8 Replies

4. UNIX for Dummies Questions & Answers

Brackets, parentheses in Shell Scripting

Hello, I have done a great deal of research on this and still cannot come to an understanding of the following: In if-else statements in shell scripting I have seen examples of single brackets, double brackets, single parantheses, and double paratheses. When should each one be used? And... (2 Replies)
Discussion started by: mojoman
2 Replies

5. Shell Programming and Scripting

Double square brackets question

Hi, I just came across an interesting shell script syntax like the one below: ] && (trap 'rm -rf ${WORK_DIR}/*.$$; echo "\n\nInterrupted !!\n\n"; exit 4' 1 2 3 15) Can someone please explain the code snippet above? The trap command bit is fine but ] && is the hazy part. Generally we use an... (2 Replies)
Discussion started by: King Nothing
2 Replies

6. Shell Programming and Scripting

why double brackets for array loop?

Dear All, Recently got some sample from internet search for array handle in shell script, and I can not understand why we need to put double brackets when extracting array values by this way : for ((i=0; i<${#admins}; i++)); do echo "${admins}" done if modify from double to single... (2 Replies)
Discussion started by: tiger2000
2 Replies

7. Shell Programming and Scripting

Use of double square brackets in ksh

Hi First apologies if this has been raised before. I've got the following in a ksh script: if ] For some reason this does not work. But if I remove the double square brackets to: if This works. I thought ksh supported the ]. Or is there more to it? Thanks in advance. (3 Replies)
Discussion started by: tsu3000
3 Replies

8. Shell Programming and Scripting

WHy the double square brackets?

One of the senior administrators gave me a shell script to modify and it begins as follows: if ] && ] {more code follows} Why the double square brackets? (10 Replies)
Discussion started by: mojoman
10 Replies

9. UNIX for Dummies Questions & Answers

why put double square brackets in an if clause?

what is the rationale behind putting double square brackets in an if clause? for e.g. if ] || ] || ]; then echo some fields are null fi (5 Replies)
Discussion started by: napolayan
5 Replies

10. Shell Programming and Scripting

single or double quote in SED

i m trying the following command but its not working: sed 's/find/\'replace\'/g' myFile but the sed enters into new line # sed 's/find/re\'place/g' myFile > I havn't any idea how to put single quote in my replace string. Your early help woud be appreciated. Thanx (2 Replies)
Discussion started by: asami
2 Replies
Login or Register to Ask a Question