Sharing knowledge about command line and shell script.

Focus on content, no distractions.

code snippet

Checking if an array contains an element in bash

When working with arrays it’s quite common to face the need to check if an array includes a certain element. Although we can have arrays in bash, we don’t have a specific method to check that. In this article we’re going to address this problem using a not so known bash feature. TL;DR If you just want the solution and don’t care about how it works, here it is (the explanation comes right after):...

December 12, 2020 · 5 min · meleu
joinBy() function

How to join() array elements in a bash script

Some languages (like JavaScript and PHP) have a function like join() or implode() to join the elements of an array separating them by a character or a string. Let’s do the same in pure bash. As a bonus, at the end this article you’ll know: the differences between $*, $@ how the IFS variable influences the behaviour of $* a variable expansion/substring replacement technique a printf “hack” how to find help from your system (it can be faster than googling) how to search inside a huge man page (like the bash’s one) how to quickly get help for the bash-builtin commands (rather then searching inside that huge man page) Let’s go!...

December 5, 2020 · 8 min · meleu

What the #! shebang really does

What exactly happens when we run a file starting with #! (aka shebang), and why some people use #!/usr/bin/env bash. How the #! works The #! shebang is used to tell the kernel which interpreter should be used to run the commands present in the file. When we run a file starting with #!, the kernel opens the file and takes the contents written right after the #! until the end of the line....

November 28, 2020 · 5 min · meleu