Table of Contents

What Is List And Arrays In Bash

There is no data structure in array with name List. But List is generally refer to the "list of items" in Bash Loops.

Let us look at an example. Let us say we want to print 5 numbers that is from 1 to 5. Also checkout how to run bash in Python.

for x in {1..5}
do
    echo $x
done
> 1
> 2
> 3
> 4
> 5

In above code, 1..5 is a list of numbers but {1..5} is an array containing numbers.

Lets create empty files using same code above.

for x in {1..5}
do
    touch file$x
done


ls file*
> file1  file2  file3  file4  file5

How To Loop Through List Of Files in Bash

Now in bash, we can loop through the list of files and find the size of each file.

for x in `ls file*`
do
      ls -lrt $x
done
> -rw-r--r-- 1 root root 0 Dec 17 13:52 file1
> -rw-r--r-- 1 root root 0 Dec 17 13:52 file2
> -rw-r--r-- 1 root root 0 Dec 17 13:52 file3
> -rw-r--r-- 1 root root 0 Dec 17 13:52 file4
> -rw-r--r-- 1 root root 0 Dec 17 13:52 file5

As we see above, we are going through the list of files using bash "for loop" and printing the size of each file.

What is Arrays in Bash

Any variable declared in bash can be treated as an array. Use the () around the values to declare an array.:

files=(file1 file2)

How To Index Array In Bash

files is an array. We can index array in bash using curly brackets as shown below...

echo ${files[0]}; echo ${files[1]}
> file1
> file2

Loop Through Array in Bash

Lets loop through above array in bash.

for f in ${files}
do
    echo "$f"
done
> file1

How To Loop Through All Values In Bash Array Using Operator @

Note instead of two files, we just got "file1", To loop through the array use index @ that means all. Lets try again.

for f in ${files[@]}
do
    echo "$f"
done
> file1
> file2

Lets create an array using "ls" in bash.

files1=$(ls file*)
echo ${files1[0]}
> file1 file2 file3 file4 file5

Checkout the difference, now all the files are part of index 0 array. If we do files[1], we would get nothing.

echo ${files1[1]}

Lets loop through above array in bash.

for f1 in ${files1}
do
    echo "$f1"
done
> file1
> file2
> file3
> file4
> file5

Note, we did't have to use @ to loop through all the files here because there is single value in index $files[0] and by default bash is treating space as separator between the list of files.

But wait, can we convert "$files1" to an bash array.

Yes, we can using following...

files2=(${files1[@]})
echo ${files2[@]}
> file1 file2 file3 file4 file5

Lets loop through above array now.

for f in ${files2[@]}
do
    echo "$f"
done
> file1
> file2
> file3
> file4
> file5

Lets check if we can access using index.

echo ${files2[4]}
> file5

There you go, it is working now.

Properties Of Bash Arrays

Lets talk about few Properties of Bash Arrays

How To Find Length Of Array In Bash

If you want to find the length of array in bash, use #, as shown below.

echo ${#files1[@]}
> 1
echo ${#files[@]}
> 2

How To Print Array Indices In Bash

echo ${!files[@]}
> 0 1

How to append values in array in bash

Lets append file3 to our array files

files+=file3

Lets check our array values now.

echo ${files[@]}
> file1file3 file2

As we see above, we need to add space too to separate the values in array.

files=(file1 file2)
files+=" file3"
echo ${files[@]}
> file1 file3 file2

Ok it is better now but as we see above order of values inside array in bash can't be maintained.

Wrap Up!

I hope above examples would help you understand about Bash arrays and lists.

Related Topics:

bash check if directory exists

bash functions bash function arguments

Related Posts

1