How to Create a List in Bash?

Bash is a popular programming environment for automating tasks in Linux. There is no data type named “list” in the bash script, but the list can be generated which can be utilized when needed. The list is the collection of elements that can be generated using loops such as for and while or arrays. 

This post will demonstrate various examples to create lists in Bash.

  • How to Create Lists in Bash?
    • List of Files Through for Loop
    • List of Numbers Through while Loop
    • List For the Name of Weekdays Using Arrays

How to Create Lists in Bash?

To generate the list in the bash script using loops (for and while) and arrays, go through the below-mentioned examples:

Example 1: List of Files Through for Loop

The following script will generate the list of files with their permissions using the for loop:

#!/bin/bash

for a in `ls file*`
do
      ls -l $a
done

The script is defined as below:

  • The “for” loop with “a” variable is used to read all files having the name “file” using the command “ls” command.
  • After that, the “do” section contains the permission for each file name stored in the “$a” variable using the “ls -l” command.

Run the script file using the bash command:

$ bash script.sh

The permission for all files having the name “file” is listed.

Note: For gaining more information about the for loop in bash, navigate to our latest article here.

Example 2: List of Numbers Through while Loop

The following script will generate the number of lists from 1 to 5 in the terminal:

#!/bin/bash
i=1
while [ $i -le 5 ]
do
    echo "$i"
    (( i++ ))
done

The script is defined below: 

  • The “i” variable is initialized with 1.
  • The “while” loop is used to check the condition that variable “i” is less than 5 to execute the iterations.
  • In the “do” section, the “i” variable prints through the echo command and increments in the variable using the “((i++))” expression.

Run the script file to obtain the results:

$ bash script.sh

The number list from 1 to 5 is generated as shown above.

Note: To know more about the while loop in bash, check out our latest guide here

Example 3: List for the Name of Weekdays Using Arrays

The following script will generate the list of weekdays in the bash script:

#!/bin/bash

Array=(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)


  echo "The list of array is: ${Array[@]}"

The script is defined in the enlisted format: 

  • An “Array” is initialized by defining the name of days in the week.
  • Then, the echo statement prints the list of arrays using “${Array[@]}”.

Run the script file using the bash command:

$ bash script.sh

The list of weekdays has been displayed in the terminal.

Note: To gain in-depth knowledge about the arrays in bash, reach out to our article here

Conclusion

There is no existence of a list data type in the bash script, but the list can be generated using the for and while loops or arrays. The user can generate lists and store different kinds of data such as a list of files, numbers, or weekdays. This write-up has answered the query “What is a List in Bash”.