Fundamentals 8 min read

Master Linux: Run Multiple Commands in One Line with ;, &&, and ||

This guide explains how to chain multiple Linux terminal commands on a single line using the semicolon, logical OR, and logical AND operators, provides syntax examples, demonstrates combined usage, and shows how to automate sequences with shell scripts for efficient command‑line workflows.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux: Run Multiple Commands in One Line with ;, &&, and ||

Running Multiple Linux Commands in One Line

Linux terminals allow chaining commands using three operators: the semicolon ( ;), logical OR ( ||), and logical AND ( &&). Each operator determines whether subsequent commands run based on the success or failure of the preceding command.

Semicolon ( ; ) Operator

The semicolon executes commands sequentially regardless of whether the previous command succeeded. commandA ; commandB Example: display the current user name and then the system hostname.

whoami ; hostname

OR ( || ) Operator

The OR operator runs the second command only if the first command fails (returns a non‑zero exit status). commandA || commandB Example: create a file only if it does not already exist.

find . -name linuxmi.txt || touch linuxmi.txt

AND ( && ) Operator

The AND operator runs the second command only if the first command succeeds (returns a zero exit status).

mkdir linuxmi && cd linuxmi

Combining Multiple Operators

Operators can be combined to meet more complex execution criteria. For instance, you can run two commands (B and C) only if command A fails. commandA || commandB && commandC Example: create a directory named linuxmi only when it does not already exist.

find . -name linuxmi1 || echo "Directory not found" && mkdir linuxmi

Running Multiple Commands via a Shell Script

Shell scripts let you automate a series of commands. Create a file with a .sh extension, make it executable, and run it.

chmod +x linuxmi.sh
./linuxmi.sh

Sample script that updates the system:

#!/bin/bash
sudo apt update && sudo apt upgrade -y

Efficient Command Execution in Linux

Using these operators simplifies many command‑line tasks, helps beginners master the shell, and enables more powerful and concise workflows.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellScriptingoperatorsterminal
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.