0

I'm working on best practice for my bash scripts. At the top of each file, I put the following so it exits if any command produces an error:

set -o errexit

The problem is it doesn't help with debugging. I'm looking for a way of logging what command failed (i.e. the command and arguments that were run), and what line the error occurred on, and in which file.

What's a good way of doing this? E.g by a small set of commands at the top of each bash script? Perhaps having a central file that is used for all scripts to log such errors? What might those commands be?

Also, is it a good idea putting that in every script that needs it? Or should a single file with it be source'd into every script that needs it? Or perhaps there's a different way?

2
  • Put set -x at the top. This will display all the lines as they execute. Commented Oct 9, 2016 at 12:52
  • 1
    There's at least some question about whether set -e is really a best practice, because it might make you sloppy about handing error conditions yourself and it doesn't always work as expected. see here for example Commented Oct 9, 2016 at 12:58

1 Answer 1

1

You have few options:

  • Use at the top of your script.sh file: #!/bin/bash -x
  • Use set -x option in script.sh
  • Use -x from command line: bash -x script.sh
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.