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 | || |
|| |
|| |
build && test && deploy stops at the first failure
mkdir out || echo "failed" message only when mkdir failsWindows (CMD)
@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.txtblue 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 }.
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 buildbuild created
build already exists
step 1
step 2
did the last command succeed? False
True
$?isTrueorFalsefor 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 buildprintsTrue; use it in anifrather than with&&.
Linux & macOS (bash/zsh)
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 && pwdblue 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 acdinside the parentheses doesn't change your current folder afterwards.- In scripts,
set -estops 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:
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?
- Always
- Only if
makesucceeded (exit code 0) - Only if
makefailed - In parallel with make
Which operator runs the next command unconditionally in CMD?
- &&
- ||
- &
- ;
Which PowerShell version is needed for cmd1 && cmd2?
- Windows PowerShell 5.1
- PowerShell 7 or later
- Any version
- None, PowerShell has no &&