0

I'm not sure I address this problem correctly, but I've search and search over the net and found nothing, yet.

I'm writing a bash script and I want to see what the script is really doing when being executed, like a log of all the commands, one by one, what are executed - this way, I'll be able to see why my script is falling.

Note : I've post similar question in the past, someone told me I run my bash script with sh -xe script.sh but it doesn't give me enough information to debug properly.

Please advise. Thanks!

  • Would it make sense to write some information to the syslog, as part of your script? – noam Jul 10 '20 at 19:15
  • @noam: As far as I understand,no. I do not want to write the log myself, with `logger`, I want to see what are all the commands that my script is running. In fact,I got a curl with a variable in it, and it always giving me an error. For testing purpose, I'm able to run it by writing the content of the variable manually, and it works, but I need it with a variable.And I've many other curl with variables, they all works fine, but a specific one always give me an error and it makes no sense. So I'm trying to see what is the final command exactly, that the bash terminal is receiving from my script – autodidacte Jul 10 '20 at 19:28
  • Where do you want to see the results? You can add some debug prints, which is the same as logging messages, except that they will be printed to STDOUT. – noam Jul 10 '20 at 19:37
  • [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – glenn jackman Jul 10 '20 at 19:40
  • Adding `set -x` at the beginning of the script, as someone wrote in a deleted (why?) comment work perfectly. Thanks! It displays command as they as executed in the terminal, that is exactly what I needed - and now this part of my script is debugged, and working, and I can move on :) Thanks to everybody who take their time to help me. – autodidacte Jul 10 '20 at 20:08
  • 1
    Putting `set -x` in your script is **exactly** the same thing as running `bash -x yourscript`, which is what you're saying someone already told you to do (except that they were suggesting `set -e` as well, which is [generally a bad idea](http://mywiki.wooledge.org/BashFAQ/105), and suggesting use of `sh`, which [is not bash](https://mywiki.wooledge.org/BashGuide/Practices#Choose_Your_Shell)). – Charles Duffy Jul 10 '20 at 20:13
  • BTW, I'd strongly suggest setting a `PS4` that tells your shell you want it to log source file name, function, and line number. Very handy to have when things get complicated. – Charles Duffy Jul 10 '20 at 20:15

1 Answers1

1

Adding set -x at the beginning of the script displays, in the terminal, all the commands sent by the script as the terminal received it. It was exactly what I needed and it working perfectly.