DEV Community

Donald Feury
Donald Feury

Posted on • Originally published at donaldfeury.xyz on

4 2

How to do conditional execution in Bash

How to do conditional execution in Bash

In Bash scripts, any command following a && will only execute if the previous conditional or command succeeded (returned 0)

The following script will only print out the variable if the conditional succeeds:

[[-z "$1"]] && echo "$1"

Enter fullscreen mode Exit fullscreen mode

On the other hand, any command following a || will only execute if the previous conditional or command failed (returned a non zero)

The following script will only print out the variable if the conditional fails:

[[-z "$1"]] || echo "$1"

Enter fullscreen mode Exit fullscreen mode

Related


Did you find this information useful? If so, consider heading over to my donation page and drop me some support.

Want to ask a question or just chat? Contact me here

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more