What is the difference between `;`, `&&`, `||` and `&` between commands?
BeginnerConceptShells, scripts and execution
Answer
a ; brunsbaftera, regardless of result.a && brunsbonly ifasucceeded (exit 0).a || brunsbonly ifafailed.a & brunsain the background andbimmediately.
mkdir -p /srv/app && cd /srv/app || exit 1
systemctl is-active --quiet nginx || systemctl restart nginx
a && b || c is not if/else: c also runs when b fails.
More on Shells, scripts and execution
What is a shell, and how is Bash different from sh, dash and zsh?BeginnerWhat does the shebang line do, and why prefer `#!/usr/bin/env bash` over `#!/bin/bash`?BeginnerWhat is the difference between `./script.sh`, `bash script.sh` and `source script.sh`?BeginnerWhat happens when you type a command name? How does Bash decide what to run?AdvancedWhy must `cd`, `export` and `read` be builtins rather than external programs?IntermediateWhat is the difference between a login, interactive and non-interactive shell, and which startup files run?Intermediate