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)
$file
is a parameter that should be7z x -- "$file"
andmv -- "$file" "$new_file"
(NB some people seem to think that${var}
is equivalent to"$var"
) – Chris Davies Mar 09 '24 at 19:35set -e
at the beginning of the second example? Would it then behave the same as the first snippet? – Meto Mar 09 '24 at 21:42$A
as"$A"
prevents shell expandingA
into multiple words. Writing$A_B
as${A}_B
prevents shell trying to expand an unknown variableA_B
instead of expandingA
and appending_B
. Many of the extended expansions (array, substring, trimming) require the{..}
to control the internal syntax. – Paul_Pedant Mar 10 '24 at 10:117z … && mv …
will only runmv
if7z
succeeds, but will continue running aftermv
if7z
fails; whereasset -e; 7z …; mv …
will exit if any command fails, includingmv
, and anything followingmv
won’t run if7z
ormv
fails. – Stephen Kitt Mar 10 '24 at 12:46