Iterating over a range of dates in a shell script
Start with the query and work backwards.  A poorly-written, poorly-performing query is not a good foundation to build on.

Here is your query (re-formatted to more readable)

SELECT il.BatchID
FROM tbl_batch_table AS il
WHERE
  il.batch_name LIKE '%$batchdate%'
  AND il.batch_name LIKE '%$batchwupos%'
  AND il.status='SUCCESS'


This query will not be able to leverage an index on batch_name because you are using a LIKE condition that starts with a wild card character.

Can you split apart the batch date information in your DB table into it's own column?  That would give you the ability to do things like:


Query on a date range AND use index to do so.
Perform date-based math (adding/subtracting days)


Imagine this sort of query:

SELECT
    il.batch_date,
    /* Create calculated field representing processing date four days
       after batch date */
    DATE_ADD(il.batch_date, INTERVAL 4 DAY) AS processing_date,
    il.BatchID
FROM tbl_batch_table AS il
WHERE
  /* Filter for records batched in last 4 days */
  il.batch_date &gt;= DATE_SUB(CURDATE(), INTERVAL 4 DAY)
  AND il.status='SUCCESS'
/* Order your results */
ORDER BY il.batch_date ASC, il.BatchID ASC


Here you can leverage an index on batch_date for WHERE clause and ordering.  You can also totally eliminate having multiple queries as you are getting records for the entire range of dates at once.

If you wanted to go with the suggestion from other answer and aggregate information for each date with concatenated results, you could add GROUP BY and GROUP CONCAT to this query like follows:

SELECT
    /* Create calculated field representing processing date four days
       after batch date */
    DATE_ADD(il.batch_date, INTERVAL 4 DAY) AS processing_date,
    GROUP_CONCAT(il.BatchID) AS batch_ids
FROM tbl_batch_table AS il
WHERE
  /* Filter for records batched in last 4 days */
  il.batch_date &gt;= DATE_SUB(CURDATE(), INTERVAL 4 DAY)
  AND il.status='SUCCESS'
GROUP BY processing_date
/* Order your results */
ORDER BY il.batch_date ASC, il.BatchID ASC