Tag: shell

Elegant bash conditionals

5 minute read    Published: 2021-03-02

The if-statement is a very basic thing, not just in bash, but in all of programming. I see them used quite a lot in shell scripts, even though in many cases they can be replaced with something much more elegant.

In this rather short article, I'll show how control operators can be used instead. Many probably know about this, but don't realize how to use them nicely. This will help you write cleaner shell scripts in the future.

Here is what a simple if-statements looks like in bash:

if [ expression ]
then
    command
fi

# or
if [ expression ]; then command; fi

Ughh. Let's improve!