Luke Oliff.

TIL: Chaining Shell Commands With && vs ; vs ||

·TIL·1 min read·Luke Oliff

Running commands in sequence is the backbone of CI scripts. The choice between &&, ;, and || determines whether a failure halts the pipeline or is ignored.

npm run build && npm test

&& runs the next command only if the previous one succeeded. Build fails? Test never runs. This is the standard pattern for CI pipelines.

npm run build; npm test

; runs every command regardless of exit codes. Build fails? Test still runs. Use this when you want to run everything and collect all results.

npm run build || echo "Build failed, continuing anyway"

|| runs the next command only if the previous one failed. The echo provides a message without masking the failure code.

npm run build && npm test || echo "Something went wrong"

Combined chain. Build passes and tests pass? No message. Build fails or test fails? The echo runs.

What exit code does the chain return?

The last executed command determines the exit code. Use set -e in CI scripts to fail on any error.