Check if element exists in array in bash

May 02, 2012

Reading time ~1 minute

Here’s a little function to verify if an element exists in a Bash array.

function elementExists() {
    elements=${1}
    element=${2}
    for i in ${elements[@]} ; do
        if [ $i == $element ] ; then
            return 1
        fi
    done
    return 0
}

It takes an array as first argument and the element as the second and will return 1 if the element exists in the array. You can now call the function like this:

elementExists my_array my_element

Running vim-airline with Maximum Awesome

I use [Maximum Awesome](https://github.com/square/maximum-awesome) when I code in [vim](http://www.vim.org/), it's a great starting place...… Continue reading

Quicksort implementation in Python

Published on August 04, 2014

Fibonacci generator in Python

Published on August 04, 2014