Array Variable | Shell Scripting

Array Variable | Shell Scripting

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.

  1. 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.
  2. To print all the values of array just write this command echo ${<name_of_array_variable>[<index_location>]}. For example: echo ${os[@]}
  3. 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]}
  4. 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[@]}
  5. 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[@]}
  6. We can add elements to array in this way <name_of_array_variable>[index_location]=”<value>”. For example: os[3]=’mac’
  7. We can update the data of an array in the same way <name_of_array_variable>[index_locaiton]=”<value>”. For example: os[0]=’mac’
  8. If you want to remove an element from an array then you can do it by using unset command like this unset <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,

📧 Write a mail

rahulmishra102000@gmail.com

GitHub, HackerRank