TExcelTAD
Home › Terminal & Command Line › Chaining Commands

Chaining Commands

Often you want to run several commands in one line: build and then test, create a folder and then enter it, try something and print a message if it fails. Shells let you chain commands with a few operators. The most useful ones look at whether the previous command succeeded, which every program reports through an exit code: 0 means success, anything else means failure.

Operator Meaning CMD PowerShell 7 bash / zsh
run one after the other always run the next one & ; ;
AND run the next one only if this one succeeded && && &&
OR run the next one only if this one failed || || ||
text
build && test && deploy      stops at the first failure
mkdir out || echo "failed"   message only when mkdir fails

Windows (CMD)

cmd · try it yourself
@echo off
echo red> colors.txt& echo blue>> colors.txt& echo green>> colors.txt
findstr "blue" colors.txt >nul && echo blue is in the list
findstr "pink" colors.txt >nul || echo pink is missing
findstr "pink" colors.txt >nul
echo errorlevel after the failed search: %errorlevel%
mkdir build 2>nul && echo build created
mkdir build 2>nul || echo build already exists
(echo step 1& echo step 2) > steps.txt & type steps.txt
blue is in the list
pink is missing
errorlevel after the failed search: 1
build created
build already exists
step 1
step 2
  • A single & separates commands; && and || check the exit code, which CMD stores in %errorlevel%.
  • Parentheses group several commands so they share one redirection.
  • In a batch file you will also see the older form if errorlevel 1 echo failed, which means "errorlevel is 1 or higher". See Batch Error Levels.

Windows (PowerShell)

PowerShell has always used ; to put several statements on a line. The pipeline chain operators && and || arrived in PowerShell 7; the older Windows PowerShell 5.1 rejects them, so there you write cmd1; if ($?) { cmd2 }.

powershell · try it yourself
New-Item -ItemType Directory build | Out-Null && "build created"
New-Item -ItemType Directory build -ErrorAction SilentlyContinue || "build already exists"
"step 1"; "step 2"
Get-Item missing.txt -ErrorAction SilentlyContinue
"did the last command succeed? $?"
Test-Path build
build created
build already exists
step 1
step 2
did the last command succeed? False
True
  • $? is True or False for the last command. For native programs (git, ping.exe, python) the numeric exit code is in $LASTEXITCODE.
  • Many PowerShell checks return a boolean instead of failing. Test-Path build prints True; use it in an if rather than with &&.

Linux & macOS (bash/zsh)

bash · try it yourself
printf 'red\nblue\ngreen\n' > colors.txt
grep -q blue colors.txt && echo "blue is in the list"
grep -q pink colors.txt || echo "pink is missing"
grep -q pink colors.txt; echo "exit code of the last grep: $?"
mkdir build && echo "build created"
mkdir build 2>/dev/null || echo "build already exists"
{ echo "step 1"; echo "step 2"; } > steps.txt; cat steps.txt
cd build && pwd
blue is in the list
pink is missing
exit code of the last grep: 1
build created
build already exists
step 1
step 2
/home/learner/build
  • { ...; } groups commands in the current shell; ( ... ) runs them in a subshell, so a cd inside the parentheses doesn't change your current folder afterwards.
  • In scripts, set -e stops at the first failing command, which is like putting && between every line.

The a && b || c trap

It looks like an if/else, but c also runs when a succeeds and b fails:

bash · try it yourself
true && false || echo "this runs even though the first command succeeded"
if true; then false; else echo "this does not run"; fi
echo "done"
this runs even though the first command succeeded
done

Use a real if when both branches matter. The same caution applies to &&/|| in CMD and PowerShell 7.

Exit codes everywhere

CMD PowerShell bash / zsh
Last exit code %errorlevel% $LASTEXITCODE (native), $? (true/false) $?
Exit a script with a code exit /b 3 exit 3 exit 3
Succeeded? if not errorlevel 1 if ($?) if cmd; then

Learn more: Bash Error Handling, Batch Error Levels, PowerShell Error Handling.

Test yourself

In make && make install, when does make install run?

  1. Always
  2. Only if make succeeded (exit code 0)
  3. Only if make failed
  4. In parallel with make

Which operator runs the next command unconditionally in CMD?

  1. &&
  2. ||
  3. &
  4. ;

Which PowerShell version is needed for cmd1 && cmd2?

  1. Windows PowerShell 5.1
  2. PowerShell 7 or later
  3. Any version
  4. None, PowerShell has no &&