Array Basics Shell Scripting | Set 2 (Using Loops) Last Updated : 18 Apr, 2023 Comments Improve Suggest changes Like Article Like Report It is recommended to go through Array Basics Shell Scripting | Set-1 Introduction Suppose you want to repeat a particular task so many times then it is a better to use loops. Mostly all languages provides the concept of loops. In Bourne Shell there are two types of loops i.e for loop and while loop. To Print the Static Array in Bash 1. By Using while-loop ${#arr[@]} is used to find the size of Array. bash # !/bin/bash # To declare static Array arr=(1 12 31 4 5) i=0 # Loop upto size of array # starting from index, i=0 while [ $i -lt ${#arr[@]} ] do # To print index, ith # element echo ${arr[$i]} # Increment the i = i + 1 i=`expr $i + 1` done Output: 1 2 3 4 5 Time Complexity: O(n)Auxiliary Space: O(1) 2. By Using for-loop bash # !/bin/bash # To declare static Array arr=(1 2 3 4 5) # loops iterate through a # set of values until the # list (arr) is exhausted for i in "${arr[@]}" do # access each element # as $i echo $i done Output: 1 2 3 4 5 Time Complexity: O(n)Auxiliary Space: O(1) To Read the array elements at run time and then Print the Array. 1. Using While-loop bash # !/bin/bash # To input array at run # time by using while-loop # echo -n is used to print # message without new line echo -n "Enter the Total numbers :" read n echo "Enter numbers :" i=0 # Read upto the size of # given array starting from # index, i=0 while [ $i -lt $n ] do # To input from user read a[$i] # Increment the i = i + 1 i=`expr $i + 1` done # To print array values # starting from index, i=0 echo "Output :" i=0 while [ $i -lt $n ] do echo ${a[$i]} # To increment index # by 1, i=i+1 i=`expr $i + 1` done Output: Enter the Total numbers :3 Enter numbers : 1 3 5 Output : 1 3 5 Time Complexity: O(n)Auxiliary Space: O(1) 2. Using for-loop bash # !/bin/bash # To input array at run # time by using for-loop echo -n "Enter the Total numbers :" read n echo "Enter numbers:" i=0 # Read upto the size of # given array starting # from index, i=0 while [ $i -lt $n ] do # To input from user read a[$i] # To increment index # by 1, i=i+1 i=`expr $i + 1` done # Print the array starting # from index, i=0 echo "Output :" for i in "${a[@]}" do # access each element as $i echo $i done Output: Enter the Total numbers :3 Enter numbers : 1 3 5 Output : 1 3 5 Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Array Basics Shell Scripting | Set 2 (Using Loops) P prakhargvp Follow Improve Article Tags : Misc Linux-Unix Shell Script Practice Tags : Misc Similar Reads Array Basics in Shell Scripting | Set 1 Consider a situation if we want to store 1000 numbers and perform operations on them. If we use a simple variable concept then we have to create 1000 variables and perform operations on them. But it is difficult to handle a large number of variables. So, it is good to store the same type of values i 6 min read Shell Scripting - Select Loop The select loop is one of the categories of loops in bash programming. A select-loop in the shell can be stopped in two cases only if there is a break statement or a keyboard interrupt. The main objective of using a select loop is that it represents different data elements in the form of a numbered 5 min read String Manipulation in Shell Scripting String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content 4 min read Shell Scripting - Set Command The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg 6 min read String Operators | Shell Script Pre-Requisite: Conditional Statement in Shell Script There are many operators in Shell Script some of them are discussed based on string. Equal operator (=): This operator is used to check whether two strings are equal. Syntax:Operands1 = Operand2Example:Â php #!/bin/sh str1="GeeksforGeeks"; str2="ge 2 min read Bash Scripting - While Loop A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false. Â Table of Content The Syntax of 15+ min read Bash Scripting - Array Arrays are important concepts in programming or scripting. Arrays allow us to store and retrieve elements in a list form which can be used for certain tasks. In bash, we also have arrays that help us in creating scripts in the command line for storing data in a list format. In this article, we will 7 min read Bash Scripting - Split String In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met 4 min read Shell Scripting - Rules for Naming Variable Name Variables are quite important in any script or program, so we need to understand what is the convention to name these variables. There are certain rules and standards to keep in mind while giving names to the variables in Shell scripting. In this article, we will discuss and list down all the rules 4 min read Like