A unified library to operate on multiple GNU/Linux distributions and using different shells (bash and zsh)

Guillaume Bernard a7a6b80c38 fix: error with default parameters when compress pdf 1 week ago
doc 469b9254d6 fix: use typeset instead of declare for compatibility 2 months ago
lib a7a6b80c38 fix: error with default parameters when compress pdf 1 week ago
.gitignore 1edf25e6b4 fix: fix Doxygen documentation 1 year ago
.gitlab-ci.yml 49464a154e fix: fix gitlab-ci.yml 1 year ago
LICENSE b2d834059e Add Licence and README.md 2 years ago
README.md 07e901d005 docs: udpate README with new logging options 1 month ago
libShell.sh 304e5b3c2d fix: error when sourcing with bash from relative path 1 month ago



libShell is a simple library that provides interfaces for some use cases such as:

  • Logging messages: through a unique class Logger, you will be able to log every message you want in your program, to the console, a file or both.
  • Package manager abstraction: a unique set a functions will allow you to update index, upgrade packages, add repositories, etc. on your system.
  • And many other functions for: ssh, rsync, git
  • If you’re using both bash and zsh, libShell will work properly.

Use LibShell

Installation on your system

You can use git to install LibShell on your system. This is the recommended mean to get the software. You can also download the archive and extract it at the desired location. This way, you will not be able to installed updates automatically using git. The recommended directory where to store the library is in .local/lib. If you want to install it system-wide, you should prefrer /usr/local/lib

Using git

git clone https://code.guillaume-bernard.fr/guilieb/libShell ~/.local/lib

In order to perform update, you’ll simply have to pull the repository from master.

Use it in your shell scripts

Without using git, you can use *libShell easily, like in the example below:

url=https://code.guillaume-bernard.fr/guilieb/libShell/archive/master.tar.gz; libShell_dir="$(mktemp -d)" && curl -s "${url}" | tar xz --strip 1 -C "${libShell_dir}" && source "${libShell_dir}"/libShell.sh

enableLogger ${ERROR} ${STDOUT}
Log ${NOTE} "Hello from $(getRunningSystemName), version $(getRunningSystemVersion)!"

If you want to stay on a specific version of libShell, you can change the url for something else, like:


And this prints on my system:

Hello from centos, version 7!

Generate documentation

Documentation is generated using Doxygen. You’ll find the Doxyfile in the doc directory.

Major functionalities

Below are the major functionalities that libShell provides:

Logger interface


The logger function is used to send messages to the script stdout. The first step is to enable it, using enableLogger. This function takes to parameters at least. The first is the log level: ${INFO} > ${NOTE} > ${WARNING} > ${ERROR} > ${DEBUG}. The second one is the output: ${STDOUT}, ${FILE}, ${STDOUT_AND_FILE}. If you choose a file, you can pass a suffix for the auto-generated logfile in /tmp.

enableLogger ${ERROR} ${STDOUT}

If you wish to retrieve the logfile:


Log messages

The Log function is used to log messages to the output. You give it a level and a message and it does the rest.

Log ${INFO} "A informative message" # Will be blue
Log ${NOTE} "A note on a spectific point" # Will be green
Log ${WARNING} "A warning on a component" # Will be yellow/orange
Log ${ERROR} "An error occured" # Will be red
Log ${DEBUG} "A debug message" # Will be magenta
Log ${CRITICAL} "A critical error occured, exiting…" # Will have a magenta background

Levels behind ${WARNING} are redirected to /std/stderr by default. In fact, the stdout is set in ${STDOUT_OUT} and stderr in ${STDERR_OUT}. You can manually change them, like by doing:

For standard output:

$ typeset -x STDOUT_OUT="standard.txt"
$ Log ${NOTE} "Message"
$ cat standard.txt

For error output:

$ typeset -x STDERR_OUT="errors.txt"
$ Log ${ERROR} "Error message"
$ cat errors.txt
Error message


Redirecting blocs output

    Log ${NOTE} "Message"
    ls -l
} > list.txt

The content of the file will exclude the message because the message is sent to /dev/stdout, the standard output for the running process. If you wish to get this output, you need to redirect it manually, to /dev/tty for instance. You can procede as follow:

$ STDOUT_OUT=/dev/tty
$ {
      Log ${NOTE} "Message"
      ls -l
  } > list.txt

Please not that no lmessage is redirected to the file list.txt. Ths one only contains the output of the ls -l command.

Package manager abstraction

libShell provides abstraction for your system package managers: yum, dnf and apt. The operations you can perform are:

  • Update the software cache (like apt update o dnf check-update --refresh)
  • Upgrade the system (like apt upgrade or dnf upgrade)
  • Install a list of packages on the system (like apt install or dnf install)
  installSoftwarePackages firefox thunderbird
  • Add a repositoy on your system. On RPM bases distributions using yum or dnf you just need to provide the repository URL. For Debian based distributions, you must give the apt line and the GPG Key URL.
  addRepository "http://www.example.org/archive jessie" "http://www.example.org/gpg-key"

Colors and formatting

You’ll be able to access all colors from terminal (the 16 basic immutable colors) using their pretty name like accessing any other variable: ${BLUE}, ${GREEN}, etc. It is included in the colors.sh file.

It’ the same for formatting output, see formatting.sh.


libShell is distributed under the GNU GPL v3 license and comes with absolutely no warranty.