Run cd build only if mkdir build succeeds.
Do not run cd when mkdir fails.
Which one-line command is appropriate?
&& executes the right command only when the left command exits with status zero.
Detailed explanation
mkdir build ; cd buildIncorrect. A semicolon runs cd even if mkdir fails.
Incorrect. A semicolon runs cd even if mkdir fails.
mkdir build && cd buildCorrect. mkdir build && cd build conditions cd on successful creation.
Correct. mkdir build && cd build conditions cd on successful creation.
mkdir build || cd buildIncorrect. || runs the right side on failure.
Incorrect. || runs the right side on failure.
mkdir build | cd buildIncorrect. A pipe connects output and input rather than testing success.
Incorrect. A pipe connects output and input rather than testing success.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); cd "$tmp"; mkdir build && cd build; pwd; cd /; rm -rf "$tmp"Expected result
末尾が/buildの一時パスKey points
- && is an AND list
- Zero means success
- The right side runs on success only
Notes
- Environment: Bash 5.x / 一時ディレクトリ
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.