Is a newline equivalent to && in a bash script?

I’m very simply wondering if, in a bash script,
a new line is functionally 100% equivalent to &&?

e.g.:

#!/bin/bash

7z x "${file}"

mv "${file}" "${new_file}"

vs

#!/bin/bash

7z x "${file}" && mv "${file}" "${new_file}"

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)

Asked By: s.k

||

No: && only runs the command following it if the command preceding it succeeds. Thus

7z x "${file}" && mv "${file}" "${new_file}"

only renames the file if 7z completes successfully, whereas

7z x "${file}"
mv "${file}" "${new_file}"

will run mv in all cases (and fail if ${file} doesn’t exist).

See What are the shell's control and redirection operators? for details (in particular, it describes how new lines are also not quite the same as ;).

Answered By: Stephen Kitt
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.