Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm very new to Unix and am trying to learn basic awk scripting - during which I keep coming across the word "array", and "associative arrays". I have no clue as to what this is; and don't tell me to RTFM because I have read every possible written explanation on it, and I simply don't understand what they're saying. (I barely understand what a variable is).

Can someone please explain in clear simple stupid language (and if you use computerese, please define the terms) for a non-computer person?

What is an array? Why would I need to use one? How do I use an array or express one whatever it is in an awk script?

share|improve this question
There is no such thing as “an array in Unix”. Arrays are a programming concept. Each programming language implements this concept in a certain way. Programming isn't learnt in a day, it's normal to not be able to do complex things when you're starting. I recommend a structured approach to learning: get a book (about awk if you decide to start with awk) and read it chapter by chapter, doing the exercises. The first programming language is the hardest. – Gilles Feb 13 '14 at 20:42
Take Gilles advice, he knows what he's talking about! – slm Feb 13 '14 at 20:57
Array is also called matrix in Mathematics. Is english your native language? – enedil Feb 13 '14 at 21:38
Array == list, wherever you see the word array you can replace it with list, it is just a collection of items. – terdon Feb 14 '14 at 14:36
Thanks for the sound advice. It really helps. – Müt Feb 16 '14 at 13:15
up vote 7 down vote accepted

What's an array?

Arrays are indexed using numbers, they usually start at 0 and go to N-1 the number of elements in an array.

What's an associative array?

Associative arrays are a key value pair, often times called a hash. Instead of using a fixed integer to index the array, you use a value, a string, to identify each element in the associative array.

Arrays in Unix

Arrays (both types) are used throughout many of the tools in Unix. AWK and Bash are 2 that you'll likely see them both used if you do any scripting work. Programming languages such as Perl, Python, and Ruby also have these data types too.

You can check out this page that shows examples of both types of arrays in AWK, titled: AWK Arrays Explained with 5 Practical Examples.


array script - array.awk

$ cat array.awk 
#!/usr/bin/awk -f

some_array[1] = "Hello"
some_array[2] = "Everybody"
some_array[3] = "!"
print some_array[1], some_array[2], some_array[3]

Sample run:

$ ./array.awk 
Hello Everybody !

associative array - assoc_arr.awk

$ cat assoc_arr.awk 
#!/usr/bin/awk -f

  debts["Kim"] = 50
  debts["Roberto"] += 70
  debts["Vic"] -= 30
  print "Vic paid 30 dollars, but still owes", debts["Vic"]

Sample run:

$ ./assoc_arr.awk 
Vic paid 30 dollars, but still owes -30

Why would I need to use one?

Arrays (both types) are extremely useful when dealing with sets of data that are related. If I have a bunch of temperature measurements from a city for say 1 month. An array would be ideal for storing these values. Storing them in an array allows the programmer to deal with them as a related set of things. If I just stored them in variables it would be much more difficult to write code to operate against the set if say I wanted to convert them from Fahrenheit to Centigrade.

Additionally there is meta data that you get for free by using this type of data structure. They automatically contain the number of elements in them, so if I needed to find out how many values were collected for the month, I'd simply have to find out the "size" of the array, or count the number of elements in it, to acquire this information.


share|improve this answer
slm- Thanks for your help and for taking the time and the effort you did to answer my question. It's not only concise, simple, and clear, it's actually making starting to make sense to me now. ;) – Müt Feb 16 '14 at 12:53
@mutehorn - glad to hear. If your Q has been resolved feel free to mark this as the accepted A so that others know your issue's been resolved. – slm Feb 16 '14 at 12:58
done! thx again. i had a splitting migraine from staring at awk for 3 days and nothing to show for it but more questions. this definitely helped a lot. ;) – Müt Feb 16 '14 at 13:23
@mutehorn - you're very welcome, awk can do that to you. It's probably one of the harder languages to pick up, it's structure can be confusing in the beginning. Thanks for the Q, and good luck on your programming journey. – slm Feb 16 '14 at 13:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.