The ksh shell supports two types of arrays:
- Indexed array
- associative array
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | Ksh |
Time | 5m |
Syntax
You can define array as follows either as an associative array or to be an indexed array.
Syntax for an indexed array
arrayName[0]=value1 arrayName[1]=value2 arrayName[3]=value3
OR use set command as follows:
set -A arrayName value1 value2 value3
Syntax for an associative array
typeset -A employees employees[sirname]="value1" employees[firstname]="value"
OR
set -A arrayName val1 val2 val3
Examples for ksh indexed array
For example, create an array called characters with three values as follows:
set -A characters Mugen Jin Fuu
To print first value, enter:
echo ${characters[0]}
To print 3rd and last value, enter:
echo ${characters[2]}
To print all values, enter:
echo ${characters[@]}
To count number of items in an array called characters, enter:
echo ${#characters[@]}
You can use for loop as follows to iterate through all values:
for i in ${characters[@]}; do echo "Samurai Champloo character - $i"; done
Sample outputs:
Samurai Champloo character - Mugen Samurai Champloo character - Jin Samurai Champloo character - Fuu
You can add two more items as follows to exiting array:
characters[3]="Sunflower-Samurai" characters[4]="Detective-Manzo"
Sample Shell Script
#!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers[@]} do echo $i done
Sample outputs:
Fig.01: Ksh for loop array ns demo
Ksh associative arrays examples
## set associative array ## typeset -A ns ## Add values ## ns[home]="192.168.1.254" ## home dns server ip ns[isp]="123.1.2.254" ## my isp dns server ip ns[google]="8.8.8.8" ## google public dns server ip ## Okay, display values # print "${ns[home]}" print "${ns[isp]}" print "${ns[google]}" ## Count number of items in an array # print "${!ns[@]}" ## Use for loop to print all items in an array## for i in "${!ns[@]}" do echo "ns[$i] value is ${ns[$i]}" done
Sample outputs:
ns[google] value is 8.8.8.8 ns[home] value is 192.168.1.254 ns[isp] value is 123.1.2.254
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 7 comments so far... add one ↓
🐧 7 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
If i need to print the next value in the array if a certain condition is verified
for example:
for i in “${array[@]}”
do
if(….) then
echo $(i+1)
fi
done
$(i+1) is wrong what can i do?
Hi Nisrine,
I hope my necromancing is welcome here, in case your issue is still relevant.
$(ì+1) should actually be $((i+1)), since double parentheses evaluate mathematical expressions.
I hope this helps someone!
Jason
Thank you for ur reply! working!
Wow she actually used your solution after 1 year :-P
Excellent just what i needed for looping through an array
Useful as f*ck!
If this were on StackExchange, +1000 for referencing Mugen, Jin, and Fuu.
Very helpful!