Max Glenister

Software & Servers • 2 min read Plain text

Awesome Bash arrays

Just some examples of how to do things with Bash arrays, and their analogous examples in Javascript.

Defining arrays

Javascript

var array = ["one","two","three","four","five"]

Bash

array=("one" "two" "three" "four" "five")

Looping through an array

Javascript

for (var i=0; i<array.length; i++) {
    console.log(array[i]);
}

Bash

for (( i=0; i<${#array[@]}; i++ ));
do
    echo ${array[$i]}
done

Getting the length of an array

Javascript

array.length; // output: 5

Bash

${#array[@]} # output: 5

Selecting a specific item in an array

Javascript

array[2]; // output: "three"

Bash

${array[2]} # output: "three"

Selecting a range of items in an array

It’s worth noting that in the Javascript example, we specify the start index and end index as parameters, but in the Bash example we specify the start index and length.

Javascript

array.slice(1,3); // output: ["two", "three"]

Bash

${array[@]:1:3} # output: two three four

Length of a single item in the array

Javascript

array[2].length; // output: 5

Bash

${#array[2]} # output: 5

Re-assign the value of an index in the array

Javascript

array[2] = "ten";

array; // output: ["one", "ten", "three", "four", "five"]

Bash

array[2]="ten"

${array} # output: one ten three four five

Add to end of an array

Javascript

array.push("nine");

array; // output: ["one", "two", "three", "four", "five", "nine"]

Bash

array=(${array[@]} "nine")

${array} # output: one two three four five nine

# bonus

array[${#array[@]}+1]="nine"

Concatenate two arrays

Javascript

var array2 = ["apple", "orange", "banana"];

var new_array = array.concat(array2);

new_array; // output: ["one", "two", "three", "four", "five", "apple", "orange", "banana"]

Bash

array2=("apple" "orange" "banana")

new_array=(${array[@]} ${array2[@]})

${new_array} # one two three four five apple orange banana
  • Software & Servers · shares #guide
    Merge Commits Are Just Noise

    Run git log --graph on most team projects and you’ll see the problem. Parallel lines everywhere, merge commits that just say “Merge branch ‘feature/x’ into develop”. None of it useful. It’s noise. (Merges aren’t the only source: an hourly GitHub Action was quietly doing the same to my own history.) I’ve been using a rebase-based workflow for years to avoid this. The history stays linear and the diffs stay readable. This post covers both the argument and the specific branch structure I use for team feature work.

    June 2026 • 7 min read
  • Blog & IndieWeb · shares #programming
    Reducing commit log noise caused by a GitHub Action

    I have a “now playing” widget on my /now page that shows the last track scrobbled to Last.fm. It’s powered by a GitHub Actions workflow that polls the Last.fm API every hour and commits the result to the repo. It worked fine, but quietly polluted my commit history.

    February 2026 • 4 min read
  • Blog & IndieWeb · shares #programming
    A Bit of a Bodge To Make MASSIVE Emoji

    I’ve been fiddling with an instance of Known over on social.omgmog.net. As part of my fiddling I’ve been trying out some of the plugins for Known. I came across the “Render Emoji Unicode” plugin but it was using an old library, and referencing an old version of Twemoji, so I updated it to use the latest Twemoji library.

    May 2022 • 3 min read

Discussion

No interactions yet

Join the conversation

Reply from your own site with a Webmention (how?).

Webmentions last fetched 17 November 2026, 13:05