0% found this document useful (0 votes)
12 views

Arrays

This document discusses arrays in shell scripting. It defines what an array is, how to declare and initialize arrays, extract elements from arrays using indexes and offsets, concatenate and modify arrays by adding/removing elements, and deleting entire arrays.

Uploaded by

Ggg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Arrays

This document discusses arrays in shell scripting. It defines what an array is, how to declare and initialize arrays, extract elements from arrays using indexes and offsets, concatenate and modify arrays by adding/removing elements, and deleting entire arrays.

Uploaded by

Ggg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

ARRAYS

DEFINITION OF ARRAY
 An array is a variable containing multiple values of same
data type.
 No limit on array size.

 Array index starts with zero.


DECLARATION AND INITIALIZATION
 Syntax:
name[index]=value
 name is any valid name of array.

 Index could be any number or expression that evaluates


to number equal to or greater than array.
 To access an element from an array, use curly brackets.

${name[index]}
EXAMPLE
#! /bin/sh

u[0]='Debain'
u[1]='Red Hat'
u[2]='Ubuntu'
u[3]='Suse'
echo "${u[2]}”
INITIALIZATION DURING DECLARATION
 Syntax:
declare –a arrayname=(element1 element2 ..)
 declare –a declares an array and all elements in
parenthesis are elements of an array.
 Example:

#! /bin/sh
declare -a u=('Debain''Red hat')
echo ${u[@]} or echo ${u[*]}
LENGTH OF AN ARRAY
 We get length of an array using special parameter $#
${#arrayname[@]} – length of array
 Example:

#! /bin/sh
declare -a u=('Debain' 'Red hat' 'Suse' 'Fedora')
echo ${#u[@]}
echo ${#u}
#! /bin/sh
declare -a u=('Debain' 'Red hat' 'Suse' 'Fedora')
echo ${#u[@]}
echo ${#u}
EXTRACTION BY OFFSET AND LENGTH
FOR AN ARRAY
 Example:
u=(‘Debian’ ‘Red Hat’ ‘Ubuntu’ ‘Suse’ ‘Fedora’)
echo ${u[@]:3:2}
EXTRACTION WITH OFFSET AND LENGTH FOR
A PARTICULAR ELEMENT OF AN ARRAY

 Example:
#! /bin/sh
u=('Debain' 'Red hat' 'Suse' ' Fedora' 'UTS')
echo ${u[2]:0:4}
CONCATENATION OF ARRAYS
 Example:
#! /bin/sh
u=(‘Apple’ ‘Banana’ ‘Grapes’ ‘Cherry’)
s=(‘Rose’ ‘Jasmine’ ‘Lily’ ‘Lotus’)
us=(“${u[@]}” “${s[@]}”)
echo ${us[@]}
echo ${#us[@]}
SEARCH AND REPLACE ELEMENTS IN
AN ARRAY
Example:
#! /bin/sh
u=(‘Debian’ ‘Red Hat’ ‘Ubuntu’ ‘Suse’ ‘Fedora’
‘OpenLinux’)
echo ${u[@]/Ubuntu/SCO Unix}
ADD ELEMENT TO AN EXISTING ARRAY
Example:
#! /bin/sh
u=(‘Debian’ ‘Red Hat’ ‘Ubuntu’ ‘Suse’ ‘Fedora’
‘OpenLinux’)
u=(“${u[@]}” “AIX” “HP-UX”)
echo ${u[6]} ${u[7]}
echo ${u[@]:6:2}
REMOVE AN ELEMENT FROM AN ARRAY
Example:
#! /bin/sh
u=('Debain' 'Red Hat' 'Suse' ' Fedora' 'UTS')
unset u[3]
echo ${u[@]}
COPYING AN ARRAY
Example:
#! /bin/sh
u=('Debain' 'Red Hat' 'Suse' ' Fedora' 'UTS')
lin=(“${u[@]}”)
echo ${lin[@]}
DELETING AN ENTIRE ARRAY
 Example:
#! /bin/sh
u=(‘Apple’ ‘Banana’ ‘Grapes’ ‘Cherry’)
s=(‘Rose’ ‘Jasmine’ ‘Lily’ ‘Lotus’)
us=(“${u[@]}” “${s[@]}”)
unset us
echo ${us[@]}
DELETING AN ENTIRE ARRAY
 Example:
#! /bin/sh
u=(‘Apple’ ‘Banana’ ‘Grapes’ ‘Cherry’)
u=(“${u[@]}”)
unset u
echo ${u[@]}
echo ${#u[@]}

You might also like