Cool bash scripting trick with arrays

Jack Wallen
Jun 9, 2010
Updated • Dec 22, 2012

If you do much bash scripting then you know there are some pretty nifty tricks you can pull off with it. Bash is a very flexible tool. Most Linux users don't realize how powerful it is and rarely use it. But when you do need it, it is always there. I play around with bash a lot and employ it for a number of things. The command line is certainly my friend. From the command you do do just about anything - but sometimes the simplest thing, say counting files in a directory, can elude you. To do this as a command you would issue something like ls -1 ~/ | wc -l. Not always easy to remember. But say you could create a simple bash script that would count the number of files in a directory as well as list those files. Now that would be cool...and applicable.

In this article I am going to introduce you to a simple bash script that will do just that: Count and list the number of files in a directory. The bash script is fairly simple and takes advantage of arrays.

What is an array?

If you're not sure what an array is, fear not. An array is just a systematic arrangement of objects. This arrangement is usually in rows and columns. Of course there are much more complicated definitions of arrays...especially when applied to application programming. But for the sake of this bash script, it's safe to stop at that definition.

The script

Figure 1

The script for the array is fairly simple. Figure 1 displays how the script will look as you edit it in your favorite editor. And for your copy/paste pleasure, I give you the actual code itself:


notify-send "You have $len objects in $wd."
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++
Notice the above script takes advantage of the nifty notify-send command I recently introduced you to. What this script does is use an array to count, list, and number the files within a directory. Now if you look at the code you will see that it also lists out the files (and numbers each of them). This is not practical with the notify-send command so instead we only use the notification system to inform the user how many files are within a directory. That's fine because the full output will be within the command line.
What you need to do is copy that code into a file, save the file (let's call it array), and then give that file executable  permissions with the command chmod ugo+x array. I give it user/group/other executable permissions just in case you copy that file to /usr/bin.
Naturally this script is a bit flawed. For instance if you were to copy that script into the /usr/bin directory you could run it from any directory and it would give you all of the output you need. However, if you attempt to run that command from the run dialog (hit <Alt>F2) you would receive no output. So this script, as is, is limited to command line.
Figure 2
When you run the script from a command line the results will look like that shown in Figure 2. Here you can see both the notify-send results as well as the printed results in the terminal. Pretty  nifty little trick.
Obviously this script is a bit limited right? The real purpose of this exercise was to show you a little more advance shell script as well as how a script can interact with the notify-send command.
Final thoughts
Shell scripting is an amazingly flexible and useful tool. All of those Linux users who avoid the command line have no idea the power they are missing. With this simple script you can modify it, or use it for any number of possibilities. We'll come back to arrays in shell scripting and much, much more.

Previous Post: «
Next Post: «


  1. GA-B1-G5 said on June 10, 2010 at 4:19 am

    Would you mind sharing that wallpaper? :D

  2. David said on June 10, 2010 at 1:56 am

    I’ve stopped using bash for most of my command line programming. Instead I favour fish. The fish shell offers much more flexibility, power and consistency.

    Your given example can be accomplished with:

    count ls *

    I even have compiled it on my DNS-323 Nas.
    I highly recommend you check it out.

  3. Dan said on June 9, 2010 at 8:11 pm

    Personally I find it much easier to use the ‘find’ command for almost anything like this as you can easily find out how many files there are of a certain type or name.

    Can be done on one line also

    x=0; for i in `find . -maxdepth 1 -type f`; do let x++; done; echo $x

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.