Cyberithub

How to Print Array in Bash Shell Script

Advertisements

In this article I will go through different examples to print array in Bash Shell Script. It is the most popular scripting environment in most of the Linux Flavors. Array is the most frequently used concept in most of the Programming Languages. Here we will look at the different ways to print array in bash script.

Print Array in Bash Script

Prerequisites

You need to have a running Linux system with root access to provide execute permission on all the scripts you are going to run.

What is Array 

An array is a kind of data structure which contains a group of elements. It may contains same type of elements or may contains different types of elements.

What is Shell Scripting

A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Also Read: Function in Bash Shell Scripting with Examples

How to Print Array in Bash Shell Script 1

Print Array Containing Strings using @

Here we will create an array str and will iterate it through for loop to display the output of the array.

[root@localhost ~]#vi test.sh

#!/bin/bash
str=(Hello World)
for i in "${str[@]}"
do
echo "$i"
done

Output

[root@localhost ~]# ./test.sh
Hello
World

Note: Please make sure to provide the execute permission to the script before running it by using chmod +x test.sh

Print Array Containing Numeral Values using @

Now let's create an array num with numeral values and iterate it through for loop to get all the array values.

[root@localhost ~]#vi test.sh

#!/bin/bash
num=(1 2 3 4 5)
for i in "${num[@]}"
do echo "$i"
done

Output

[root@localhost ~]# ./test.sh
1
2
3
4
5

Print Array containing both String and Numeral Values using @

Now let's use an array containing both string and Numeral values and loop it through for loop as shown below.

[root@localhost ~]# cat test.sh
#!/bin/bash
num=(1 hello 3 world)
for i in "${num[@]}"
do echo "$i"
done

Output

[root@localhost ~]# ./test.sh
1
hello
3
world

Print Array Containing Strings using *

Here we will create an array str and will iterate it through for loop to display the output of the array using str[*]

[root@localhost ~]#vi test.sh

#!/bin/bash
str=(Hello World)
for i in "${str[*]}"
do
echo "$i"
done

Output

[root@localhost ~]# ./test.sh
Hello World

Print Array Containing Numeral Values using *

Now let's create an array num with numeral values and iterate it through for loop to get all the array values using num[*]

[root@localhost ~]#vi test.sh

#!/bin/bash
num=(1 2 3 4 5)
for i in "${num[*]}"
do echo "$i"
done

Output

[root@localhost ~]# ./test.sh
1 2 3 4 5

Print Array containing both String and Numeral Values using *

Now let's use an array containing both string and Numeral values and loop it through for loop using strnum[*]. Notice the output showing in one line.

[root@localhost ~]# cat test.sh
#!/bin/bash
strnum=(1 hello 3 world)
for i in "${strnum[*]}"
do echo "$i"
done

Output

[root@localhost ~]# ./test.sh
1 hello 3 world

Reference: Bash Shell Scripting

Leave a Comment