why double brackets for array loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting why double brackets for array loop?
# 1  
Old 07-28-2009
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[$i]}"
done

if modify from double to single bracket in for loop, I got this error:

syntax error near unexpected token `('
`for (i=0; i<${#admins[@]}; i++); do'

can someone advise why ?? Thanks in advance.
Rgds,
kevin
# 2  
Old 07-29-2009
maybe some special character it cannot understand ( without ((. You must pad it

sometime back I had similar problem using square brackets, a forumer here advised to use [[ instead of [ and it solved the problem
# 3  
Old 07-29-2009
Hi.

Because
Code:
for (( ...

is the syntax for that statement.

The meaning of single and double parentheses is different. See man ksh or man bash ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. 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

5. Shell Programming and Scripting

Array with do while and if loop

Hi All, I am trying to run a do while for an array. And in the do while, I'm trying to get a user response. Depending on the the answer, I go ahead and do something or I move on to next element in the array. So far I can read the array, but I can't get the if statement to work. Any suggestions... (5 Replies)
Discussion started by: nitin
5 Replies

6. 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

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

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... (1 Reply)
Discussion started by: ankimo
1 Replies

10. 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
Login or Register to Ask a Question