Skip to main content

Posts

Showing posts from January, 2025

Arrays in Bash Shell

  Creating and Using Arrays in Bash Shell Script In Bash scripting, arrays are used to store multiple values in a single variable. Here's a step-by-step beginner-friendly explanation of how to create and use arrays. 1. Declaring an Array You can create an array by assigning values to it using parentheses () and separating the values with spaces. Example: my_array=(apple banana cherry) Here: my_array is the name of the array. It stores three elements: "apple" , "banana" , and "cherry" . 2. Accessing Array Elements Array elements are accessed using their index , starting from 0 . You use the syntax ${array_name[index]} to access an element. Example: echo ${my_array[0]} # Outputs: apple echo ${my_array[1]} # Outputs: banana echo ${my_array[2]} # Outputs: cherry 3. Adding Elements to an Array You can add a new element by assigning a value to a specific index. Example: my_array[3]= "date" echo ${my_array[3]} # Outputs: date 4. Di...