6

Why did it take years before someone told me to add this to your bash script to see the line on which a command failed?

errorOnLine() { echo "Line $1 had an error" ; exit 1 ; }
trap 'errorOnLine $LINENO' ERR

Comments
  • 0
    What I'm hearing is "Python is a better scripting language than bash"
  • 2
    @atheist I agree for syntax, error handling, variable handling and other things. But bash is better when calling other programs and piping.
  • 1
    @happygimp0 I laughed very hard reading that statement...

    Note that the bash options are far better suited for what you have in mind.

    https://tldp.org/LDP/abs/...

    set -o xtrace

    specifically.
  • 0
    @IntrusionCM Disagree, it pollutes the output. The script should only print something when there was an error.
  • 0
    @happygimp0 ehhhh... I'd actually disagree with that.

    Have you ever had to work out how to escape some bash string stupidity?

    Python, each token is a string. you pass a list of tokens to subprocess.run. I've actually used Python's shlex.join method to work out *how* to shell escape something.
  • 2
    @happygimp0

    Don't use traps for "exception" handling.

    It will bite you. Do it properly and it will save you a lot of migraine.

    There are tons of caveats, be it functions, dependency on options like errexit and other stuff.

    You think you have an easy way, but you just opened the box of pandora.

    :)
  • 0
    Bash (and make) have its good cases - very simple scripts that doesn’t require an entire language runtime to work.

    In terms of python, use plumbum and you’ll be fine; the experience is really smooth.
Add Comment