gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
get length of array in bash
- User
get length of array in bash
- Assistant
In bash, you can find the length of an array by using the syntax
${#array_name[@]}. Here's how you can do it:# Declare an array my_array=(element1 element2 element3 element4) # Get the length of the array length=${#my_array[@]} # Print the length echo "The length of the array is: $length"In this script:
- The array
my_arraycontains 4 elements. - The length of the array is stored in the variable
lengthusing${#my_array[@]}. - Finally, the length is printed to the console.
You can run this script in your bash environment to see how it works!
- The array