Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:15:33+00:00 2026-06-15T11:15:33+00:00

I have two arrays that I want to loop in. I construct those properly

  • 0

I have two arrays that I want to loop in. I construct those properly and before going into for loop, I do echo them to be sure everything is ok with arrays.
But when I run the script, it outputs an error:

l<=: syntax error: operand expected (error token is "<="

I consulted the mighty Google and I understood it suffers from the lack of the second variable, but I mentioned earlier I do echo the values and everything seems to be OK. Here is the snippet..

#!/bin/bash
    k=0
    #this loop is just for being sure array is loaded
    while [[ $k -le ${#hitEnd[@]} ]] 
      do
      echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}"
      # here outputs the values correct 
      k=$((k+1))
    done
    k=0
    for ((l=${hitStart[k]};l<=${hitEnd[k]};l++)) ; do //this is error line..
        let array[l]++      
        k=$((k+1))
done

The variables in the for loop are echoed correctly but for loop won’t work.. where am I wrong?

#

as gniourf_gniourf answered:

“… At some point, k will reach the value ${#hitEnd[@]}, and this is
exactly when hitEnd[k] is not defined and expands to an empty string!
Bang!”

meaning error output is displayed not at the beginning of the loop, but when k has a greater value than array’s indices, pointing an index that array does not include…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:15:34+00:00Added an answer on June 15, 2026 at 11:15 am

    That’s because at some point ${hitEnd[k]} expands to nothing (it is undefined). I get the same error with ((l<=)). You should write your for loop as:

    k=0
    for ((l=${hitStart[0]};k<${#hitEnd[@]} && l<=${hitEnd[k]};l++)); do
    

    so as to always have an index k that corresponds to a defined field in the array ${hitEnd[@]}.

    Also, instead of

    k=$((k+1))
    

    you can just write

    ((++k))
    

    Done!

    Your script revised using better modern bash practice:

    #!/bin/bash
    k=0
    #this loop is just for being sure array is loaded
    while ((k<=${#hitEnd[@]})); do
        echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}"
        # here outputs the values correct 
        ((++k))
    done
    k=0
    for ((l=hitStart[0];k<${#hitEnd[@]} && l<=hitEnd[k];++l)); do
        ((++array[l]))
        ((++k))
    done
    

    Now, I’m not too sure the for loop does exactly what you want it to… Don’t you mean this instead?

    #!/bin/bash
    # define arrays hitStart[@] and hitEnd[@]...
    # define array array[@]
    
    #this loop is just for being sure array is loaded
    for ((k=0;k<${#hitEnd[@]};++k)); do
        echo "hitEnd is: ${hitEnd[k]} and hitStart is: ${hitStart[k]}"
        # here outputs the values correct 
        ((++k))
    done
    
    for ((k=0;k<${#hitEnd[@]};++k)); do
        for ((l=hitStart[k];l<=hitEnd[k];++l)); do
            ((++array[l]))
        done
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have two arrays: supplierList - that contains the int ID of
I have following two arrays. I want the difference between these two arrays. That
I have two arrays in two MYSQL tables. I want to loop through the
I want update a column in mysql with a loop. I have two arrays
I have two array and three button in segment control i want that when
I have two arrays that I would like to compare and ultimately wind up
I have two arrays that I create like this: public int GameBoard[][] = new
I have two numpy arrays that define the x and y axes of a
I have two arrays of data that I'm trying to amalgamate. One contains actual
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.