Skip to main content
2 of 3
added 286 characters in body
Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k

The exit status of a for loop compound command is that of the last command executed in it.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

That one could still return false/failure if printf fails (for instance, when stdout is to a file on a filesystem that is full).

If you want to ignore any error and your script to return true/success in any case, just add a true or exit 0 at the end of your script.

Stéphane Chazelas
  • 585k
  • 96
  • 1.1k
  • 1.7k