Array in Shell Script

#! /bin/bash

fruit=('Apple' 'Banana' 'Chicco') # array are differentiated by space

echo ${fruit[@]} #print the complete array
echo ${fruit[1]} #print a value at an index
echo ${!fruit[@]} #print all the index
echo ${#fruit[@]} #print the length of the array

fruit[10]=Grapes # directly adding value at a position
echo ${fruit[@]}

echo -----------------------------------------------------------------------------
string=HelloWorld # normal String also treated as an array at 0th index in ShellScript
echo ${string[@]} #print the complete array
echo ${string[0]} #print a value at an index
echo ${!string[@]} #print all the index
echo ${#string[@]} #print the length of the array

Leave a Comment