How to accommodate for spaces in a bash array

I am looping though a directory for a certain command; however, I do not know how to accommodate for spaces

#!/bin/bash

image=(`ls *.bmp *.jpeg *.jpg | sort`)
data=(`ls *.dat | sort`)

for ((i=0; i < ${#image[@]}; i++))
do
  echo ${image[$i]} ${data[$i]}
done

I ran this script and it returned

OIS032_OS.bmp Disc
OIS034_OS.bmp Cube
OIS035_OD.bmp 200x200_9-30-2010_OD
OIS035_OS.bmp _ILM_RNFLOb.dat
OIS036_OD.bmp OIS007_Optic
OIS036_OS.bmp Disc

I wanted the program to return this line

OIS016_OD.bmp  OIS016_Optic Disc Cube 200x200_OS _ILM_RNFLOb.dat 

How do I fix bash array to store what I need

data=(`ls *.dat | sort`) 

Answers


Don't call ls; just use pattern matching to populate the array:

shopt -s extglob    # We'll use a more complicated glob to avoid needing to sort
image=( *.@(bmp|jpeg|jpg) )
data=( *.dat )

for ((i=0; i < ${#image[@]}; i++))
do
  echo ${image[$i]} ${data[$i]}
done

Need Your Help

Modal window not showing up

javascript jquery

On my page I have two modal windows. They are opened when user clicks buttons on the page. But I also want to be able to open second window when user clicks specific button on the first modal.

VIsual Studio WinForm connecting to SQL Server Database: C# syntax to transfer the data

c# sql-server database visual-studio winforms

So I am just learning .NET, WinForms, SQL Server and C#.... I previously have used VB and MS Access, so I know this is a lot different but I am looking for a place to start.