Cancel

Array/List in Bash

Array/List is the collection of values that helps you to improve your productivity. You can create an Array/List like the following code with single/double quotes separated by space, not comma.

Run the following code to create an array/list in bash.

1
fruits=("apple" "banana" "orange" "grapes")

Return all positional arguments as a separate string.

1
echo ${fruits[@]}

Return a total number of arguments of the array, or return the length of the array.

1
echo ${#fruits[@]}

You can call indexes like this same as python, or any other programming languages.

1
2
3
echo ${fruits[0]}
echo ${fruits[1]}
# so on...

To insert/push new item at the end of array.

1
fruits+=("watermelon")

To update item at specific index.

1
fruits[3]="blueberries"

To remove item by index from the array.

1
unset fruits[2]

To remove item by value from the array.

1
fruits=( ${fruits[@]/apple/} )