Remove blank lines within a file with bash

As it was quite hard to find a proper command to replace all empty lines within a text file – including empty lines at the start and end – I wanted to take down the following command which worked for me on macOS:

sed -i -z 's/^\n*\|\n*$//g' file.txt