0

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 Mar 9, 2024 at 19:10

1 Answer 1

3

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 ;).

Kamil Maciorowski
24.4k2 gold badges69 silver badges129 bronze badges
answered Mar 9, 2024 at 19:16
5
  • 1
    Since$file is a parameter that should be 7z x -- "$file" and mv -- "$file" "$new_file" (NB some people seem to think that ${var} is equivalent to "$var") Commented Mar 9, 2024 at 19:35
  • what if we add set -e at the beginning of the second example? Would it then behave the same as the first snippet? Commented Mar 9, 2024 at 21:42
  • Isn't that a good practice to put curly braces around vars? nickjanetakis.com/blog/… Commented Mar 9, 2024 at 22:33
  • @s.k Do both. Writing $A as "$A" prevents shell expanding A into multiple words. Writing $A_B as ${A}_B prevents shell trying to expand an unknown variable A_B instead of expanding A and appending _B. Many of the extended expansions (array, substring, trimming) require the {..} to control the internal syntax. Commented Mar 10, 2024 at 10:11
  • 1
    @Meto not quite — 7z ... && mv ... will only run mv if 7z succeeds, but will continue running after mv if 7z fails; whereas set -e; 7z ...; mv ... will exit if any command fails, including mv, and anything following mv won’t run if 7z or mv fails. Commented Mar 10, 2024 at 12:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.