What special character is used to access the value of a specific element in an array in Bash?

QuestionsQuestions8 SkillsProShell ArraysOct, 20 2025
0102

In Bash, the special character used to access the value of a specific element in an array is the dollar sign $, combined with curly braces {}. The syntax to access an element at a specific index in an array is ${array_name[index]}.

Example

Here’s an example to demonstrate this:

# Declare an array
my_array=(apple banana cherry)

# Access the second element (index 1)
second_element=${my_array[1]}

# Display the second element
echo "The second element is: $second_element"  # Outputs: The second element is: banana

In this example, ${my_array[1]} is used to access the value of the second element in the my_array array.

0 Comments

no data
Be the first to share your comment!