How to convert an IPV4 netmask to CIDR

To switch from an IPV4 netmask to the equivalent CIDR it might occur to do the simplest thing, that is to use a nested "case" block or "if" to obtain the equivalent CIDR so much so that the available netmasks or CIDR are only 32 and that is


Convert a decimal number to hexadecimal from Bash

As for the faq with the conversion to binary, also in this we make use of the fantastic bash braces extensions to convert to hexadecimal.

So, always considering the bash faq on the binary we start initially by converting a decimal number between 0 and 255 which corresponds to a hexadecimal digit between 0 and FF:


Convert a decimal number to binary from Bash

Suppose we are working on a bash script and that for some need, not too difficult from reality, it is essential to have to convert a number or a decimal variable into a binary number.

There are of course several solutions but the one I propose does not make use of external programs but it is bash in all respects.

First we need to know the maximum value of the binary number to be obtained in Bash or in how many bits it can be included.


Using associative arrays in bash

To declare an associative array, you must do so

declare -A mioarray

and as an index you can use a string, this means that


Using simple arrays in bash

Vedi L'articolo in Italiano

With Bash to explicitly declare an array you have to use

declare -A mioarray

To have the value of element 5 just write

echo ${mioarray[5]}

Redirect the standard error to the standard output

To redirect the standard error of a shell command, just use the following syntax:

ls file-that-does-not-exist 2>&1 | grep file

Or even better not to have on the terminal the standard error just throw it in /dev/null


Rename a group of files based on a regex

Suppose you want to rename the name of a group of files that have the following template:

and suppose you want to remove the '_tmp' middle part in all the files. We can use the command "rename" or "prename":