DEV Community

Cover image for How to run multiple command at once in Linux
FlatlinerAtheist | CLI-NERDS
FlatlinerAtheist | CLI-NERDS

Posted on Originally published at clinerds.com

How to run multiple command at once in Linux

A Linux shell can do more than run one command and wait for the next. You can chain commands, stop a sequence after a failure, provide a fallback, pass output into another program, or start work in the background. Choosing the right operator matters because each one treats success and failure differently.

These examples use Bash and similar POSIX-style shells on Linux, macOS, or Windows Subsystem for Linux. PowerShell and the classic Windows command prompt have different syntax.

Method 1: Run commands in sequence with ;

A semicolon runs each command in order, regardless of whether the previous command succeeded:

ls; pwd; whoami
Enter fullscreen mode Exit fullscreen mode

The shell lists the current directory, prints its full path, and then prints your username. Spaces around the semicolon are optional, so ls;pwd;whoami behaves the same way, but spaces make longer commands easier to read.

Use ; only when later commands are safe even after an earlier failure. For example, running independent status checks with semicolons is reasonable. Using it between changing into a directory and deleting files is not: if the cd fails, the destructive command could run in the wrong place.

Method 2: Continue only after success with &&

The logical AND operator runs the right-hand command only when the left-hand command returns an exit status of zero, meaning success:

mkdir ~/MyFolder && cd ~/MyFolder
Enter fullscreen mode Exit fullscreen mode

The shell changes into MyFolder only if mkdir succeeds. If the directory already exists, mkdir normally returns an error and the cd is skipped. If that is not what you want, use mkdir -p, which succeeds when the directory already exists:

mkdir -p ~/MyFolder && cd ~/MyFolder
Enter fullscreen mode Exit fullscreen mode

This operator is ideal for dependent build and deployment steps:

npm run lint && npm run test && npm run build
Enter fullscreen mode Exit fullscreen mode

The build runs only after the earlier checks pass. It does not prove the checks are correct, but it prevents a known failure from being ignored.

Method 3: Run a fallback with ||

The logical OR operator runs the right-hand command only when the left-hand command fails:

[ -d ~/MyFolder ] || mkdir ~/MyFolder
Enter fullscreen mode Exit fullscreen mode

The test checks whether the directory exists. mkdir runs only when the test returns a nonzero status.

You can also print a useful failure message:

curl -fsS https://example.com/health || printf '%s\n' "Health check failed"
Enter fullscreen mode Exit fullscreen mode

Be careful with longer cmd1 || cmd2 && cmd3 chains. Shell precedence and left-to-right evaluation can surprise you; explicit if statements are clearer when the logic becomes important.

Method 4: Pass output through a pipe with |

A pipe connects one command's standard output to another command's standard input:

journalctl -u ssh --no-pager | tail -n 20
Enter fullscreen mode Exit fullscreen mode

This is not the same as simply running two commands. The second command consumes data produced by the first. Pipes are useful for filtering, counting, sorting, and formatting:

printf '%s\n' beta alpha gamma | sort | wc -l
Enter fullscreen mode Exit fullscreen mode

The example sorts three names and counts the resulting lines. Check documentation for commands whose output is intended for people rather than scripts; colored or interactive output may need to be disabled.

Method 5: Start a command in the background with &

A single trailing ampersand starts a command asynchronously and returns to the prompt:

./long-report.sh > report.log 2>&1 &
Enter fullscreen mode Exit fullscreen mode

The shell prints a job number and process ID. You can inspect shell jobs with jobs and bring a job to the foreground with fg. Backgrounding does not make a command succeed, restart it after failure, or guarantee that it survives logout; use a service manager or a tool such as tmux for persistent workloads.

Do not confuse & with &&: one backgrounds a process, while the other tests success before continuing.

Group commands and control their side effects

Parentheses run commands in a subshell:

(cd /tmp && pwd)
pwd
Enter fullscreen mode Exit fullscreen mode

The cd affects only the subshell, so your original shell remains in its previous directory. This is useful when a script needs a temporary working directory.

Braces group commands in the current shell:

{ printf '%s\n' "Report generated on:"; date; } > report-header.txt
Enter fullscreen mode Exit fullscreen mode

The redirection applies to the whole group. A space after { and a semicolon before } are required in this form.

Quote data and inspect failures

Shell operators typed inside quotes are data, not command separators:

printf '%s\n' "This semicolon is text; not another command"
Enter fullscreen mode Exit fullscreen mode

Quote variable expansions as well, especially filenames: printf '%s\n' "$filename". Without quotes, spaces and wildcard characters can change the command's meaning.

Every command returns an exit status. Inspect the most recent one with:

printf '%s\n' "$?"
Enter fullscreen mode Exit fullscreen mode

Zero conventionally means success; nonzero means failure. In scripts, show a useful error and exit deliberately instead of allowing a failed command to disappear inside a long chain.

Quick reference

Operator Purpose
; Run the next command regardless of the previous result
&& Run the next command only after success
`
{% raw %}` `
& Start a command in the background
( ... ) Run a group in a subshell
{ ...; } Run a group in the current shell

For a safe default, use && between dependent steps, redirect logs explicitly, and test a chain with harmless commands before using it in automation.

Top comments (0)