How to use Array in shell scripting
How to use Array in shell scripting?
Shell supports a different type of variable called an array variable. This can hold multiple values at the same time. Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables.
All the naming rules discussed for Shell Variables would be applicable while naming arrays.
Defining Array Values
The difference between an array variable and a scalar variable can be explained as follows.
Suppose you are trying to represent the names of various students as a set of variables. Each of the individual variables is a scalar variable as follows −
#! /bin/bash
#Array variable
os=("ubuntu" "window" "kali")
os[4]="md" #to add index
os[2]="mac" #to replace index
echo $os
echo "${os[0]}"
echo "${os[@]}"
echo "${os[2]}"
echo "${!os[@]}"
echo "${#os[0]}" #length of array
string=("sfkjdf")
string[2]="love"
echo "${string[0]}"
echo "${string[2]}"
echo "${string[@]}"
echo "${!string[@]}"
0 comments
Please leave your comments...... Thanks