In other programming languages, an “array” contains similar elements. However, in the bash script, the array supports both string and numerical values in one array. These values can be easily accessed or printed through the array index number or name. The index name or number can only print the defined index value. To print all the values, an array supports the special characters or symbols at the indexed position.
Today’s post will illustrate the possible methods to use an array in sh script and print all of its values:
- Method 1: Print All Array Values Using “[@]”
- Alternative: Print All Array Values Using “[*]”
- Method 2: Print All Array Values Sequentially Using “for” Loop
Method 1: Print All Array Values Using “[@]”
In the shell script, the default array is the “indexed-array” that contains the values specified at the indexed position, i.e., array[1]=“foo.” The user can pass the “@” symbol at the indexed position that prints all the values defined in an array.
Let’s see this concept with the help of a practical example.
Example:
Create/open a “sh” script named “Program.sh” in the “nano” text editor:
$ nano Program.sh
Code:
#!/bin/bash
first_array=(Ubuntu Fedora RHEL CentOS)
echo "${first_array[@]}"
The above code description is written below:
- “#!/bin/bash” refers to the “Bash Shebang” that executes the sh script in the bash shell.
- The “first_array” specifies an array having four values at indexed 0,1,2,3.
- The “echo” command prints all the values of “first_array” due to the usage of the “@(all values)” symbol in the square brackets.
Press “Ctrl+S” to save and “Ctrl+X” to exit the terminal.
Execute the “Program.sh” script and check the output:
$ ./Program.sh
All the values of “first_array” have been printed in the terminal in the form of output.
Alternative: Print All Array Values Using “[*]”
The array also supports the “*asterisk” as an indexed value to print all the values of the particular array in the “sh” script. Let’s see its practical implementation.
The “Code.sh” script is taken as a sample sh script that contains the following lines of code:
Code:
#!/bin/bash
my_array1=(10 20 30 40 50 60 70 80 90 100)
my_array2=(one two three four five six seven eight nine ten)
my_array3=(1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th)
echo "${my_array1[*]}"
echo "${my_array2[*]}"
echo "${my_array3[*]}"
The above lines of code is described here:
- The “my_array1” specifies a first array that contains 10 integer values.
- The “my_array2” represents the second array having 10 string values.
- The “my_array3” denotes the third array containing 10 values.
- In last, the three “echo” commands print all the values of the three arrays having “*(all values)” at the indexed position in the square brackets:
Run the “code.sh” script as a command to get its output:
$ ./code.sh
The “code.sh” script has successfully printed all the declared array values in the terminal.
Method 2: Print All Array Values Sequentially Using “for” Loop
All the array values can be printed sequentially using the “for” loop. The “for” loop will iterate the number of times till all the values are printed. Let’s understand with an example
Example:
A “bash.sh” script is created that contains the following content:
Code:
#!/bin/bash
array=(one two three four five six seven eight nine ten eleven twelve)
for a in ${array[@]}
do
echo $a
done
In the above code:
- An “array1” contains 12 values that will print using the “for” loop.
- The “for” loop contains a variable “a” that stores the new value of the “array” at each iteration.
- The do and done statements represent the start and end of the “for” loop that encloses an “echo” statement.
- The “echo” statement prints each value of the “array” one by one in the terminal:
The output of the “bash.sh” script is shown below:
$ ./bash.sh
The “for” loop has printed all the values of the “array” sequentially in the terminal.
Conclusion
In Linux, all the array values declared in the “sh” script can be easily printed on the terminal by using the “@” or “*(asterisk)” symbol at the indexed position. In addition, the “for” loop also assists the users in printing all array values sequentially.
This post has covered all possible aspects of using an array in the sh script to print all values in an array.