Linux shell programming : arrays
Here is a simple array shell code:
#!/bin/bash arr[0]="zero" arr[1]="one" arr[2]="two" arr[3]="three" arr[4]="four" arr[5]="five" echo "array size = ${#arr[*]}" printf "array items\n" for item in ${arr[*]} do printf " %s\n" $item done printf "array items\n" for item in ${arr[@]} do printf " %s\n" $item done printf "array indexes\n" for index in ${!arr[*]} do printf "%3d\n" $index done printf "index: array\n" for index in ${!arr[*]} do printf "%3d: %s\n" $index ${arr[$index]} done
Output looks like this:
$ ./array1.sh array size = 6 array items zero one two three four five array items zero one two three four five array indexes 0 1 2 3 4 5 index: array 0: zero 1: one 2: two 3: three 4: four 5: five
We can declare in other ways:
- Using the pair of parenthesis:
array=( element_1 element_2 element_3 ... element_n )
-
array=( [i2]=<value> [i4]=<value> ... )
-
for item in "${arr[@]}"
-
for item in "${arr[*]}"
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
So, the the code below, we'll use those method of declaring arrays:
#!/bin/bash arr1=("zero" "one" "two" "three" "four") arr2=([4]="four" [0]="zero" [1]="one" [2]="two" [3]="three") echo "-- without double-quot" for item in ${arr1[@]} do echo $item done echo for item in ${arr1[*]} do echo $item done echo echo "-- with double-quot" for item in "${arr2[@]}" do echo $item done echo for item in "${arr2[*]}" do echo $item done
Output:
$ ./array2.sh -- without double-quot zero one two three four zero one two three four -- with double-quot zero one two three four zero one two three four
To traverse through the array elements we used two different for loops:
As we can see from the output, the "@" sign can be used instead of the "*" in constructs such as ${arr[*]}, the result is the same except expanding the array to the items within a quoted string. In this case, the behavior is the same as when expanding "$*" and "$@" and they return all the items as a single word.
While the "${arr[@]}" returns each item as a separate word:
zero one two three four
the "${arr[*]}" returns all items in a word.
zero one two three four
Generate ramdom number array with 10 elements and then sort:
#!/bin/bash #arr=(one two three four) arr=($(for i in {0..9};do echo $((RANDOM%100));done)) echo "initial array:" echo "${arr[@]}" echo "sorted array:" echo ${arr[@]} | tr " " "\n"| sort -n | tr "\n" " " echo
initial array: 76 7 66 15 23 7 9 78 83 93 sorted array: 7 7 9 15 23 66 76 78 83 93
Linux - system, cmds & shell
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization