This is a multipart blog article series where I am going to explain the concepts of shell scripting and how to write a shell script in Linux, UNIX or Mac based systems. You can also follow this tutorial blog using windows but for that you have to install a bash from.
In this article we will learn about array in shell scripting. And see how to perform different operations on array.
- We can declare array in this way
<name_of_array_variable>=(“Ubuntu” “Windows” “Kali”)
. By this way we can declare an array and store some value in it. - To print all the values of array just write this command
echo ${<name_of_array_variable>[<index_location>]}
. For example:echo ${os[@]}
- If you want to print a specific value of the array than you can use this command
echo ${<name_of_array_variable>[<index_location>]}
. For example:echo ${os[1]}
- If you want to print the index of the array than you can do it like this
echo ${!<name_of_array_variable>[@]}
. For example:echo ${!os[@]}
- If you want to pint the length of an array than you can do it like this
echo ${#<name_of_array_variable>[@]}
. For example:echo ${#os[@]}
- We can add elements to array in this way
<name_of_array_variable>[index_location]=”<value>”
. For example:os[3]=’mac’
- We can update the data of an array in the same way
<name_of_array_variable>[index_locaiton]=”<value>”
. For example:os[0]=’mac’
- If you want to remove an element from an array then you can do it by using
unset
command like thisunset <name_of_array_variable>[index_location]
. For example:unset os[2]
Reference code file for this article
So this was all about array in shell scripting. Hope you liked it and learned something new from it.
If you have any doubt, question, quires related to this topic or just want to share something with me, than please feel free to contact me.
📱 Contact Me
Twitter, LinkedIn, Telegram, Instagram,