Start your bash script with bash-x ./script.sh or add in your script set-x to see debug output.
Tagged as: bash script debug, bash shell, debug, debug shell script, debug unix script, debugging script, debugging shell script, debugging shell scripts, how to debug a shell script, how to debug shell script, shell script, shell script debug, Shell scripting, unix script debug. $ ./script.sh. Output: Now set DEBUG to off (you need to edit the file): _DEBUG=off. Run script: $ ./script.sh. Output: Above is a simple but quite effective technique. You can also try to use DEBUG as an alias instead of function. Use of set builtin command. Bash shell offers debugging options which can be turn on or off using set command. 1  set-x: Display commands and their arguments as they are executed. 2  set-v: Display shell input lines as they are read. $ ./script.sh. Output: Now set DEBUG to off (you need to edit the file): _DEBUG=off. Run script: $ ./script.sh. Output: Above is a simple but quite effective technique. You can also try to use DEBUG as an alias instead of function. Start your bash script with bash-x ./script.sh or add in your script set-x to see debug output. Additional with bash 4.1 or later: If you want to write the debug output to a separate file, add this to your script: See: http://stackoverflow.com/a/25593226/3776858. If you want to see line numbers add this: If you have access to logger command you can use this to write debug output via your syslog with timestamp, script name and line number: You can use option-p of logger command to set an individual facility.level pair to write output via local syslog to its own logfile. share | improve this answer. edited Jul 16 at 9:25.  Run the script as follows: $ ./script.sh. Output: Now set DEBUG to off (you need to edit the file): _DEBUG=off. Run script: $ ./script.sh. Output: Above is a simple but quite effective technique. You can also try to use DEBUG as an alias instead of function. -x option to debug a shell script: Below is a sample shell script .. Now, running a shell script with-x option enables the display of commands and its arguments when they are executed. Using set command debug options. Bash shell offers debugging options which can be turn on or off using set command. Below are the two options, which can be used to debug any script in a bash shell. - set-x: Print commands and their arguments as they are executed. - set-v: Print shell input lines as they are read To the tune of what NES is saying there are set flags in bash to allow for debugging. the set-x flag may be set and unset either from the terminal, or from withing your script. using + or -. here is what the set commands do: set-f Disable file name generation using metacharacters.  Using dumpStack() in a WLST script also works often, but not always. You can also enable a debug flag when starting the WLST shell: java-Dpython.verbose=debug weblogic.WLST. One other option is to use debug(true) or set('DebugEnabled', 'true') in the starting of the WLST script. This will force debug output.  Breaking out of a script [edit]. When writing or debugging a shell script it is sometimes useful to exit out (to stop the execution of the script) at certain points. You use the 'exit' built-in command to do this.  Here is the output of running this script in a debug mode using-x option for bash command. Bash shell offers debugging options which can be turn on or off using set command. Below are the two options, which can be used to debug any script in a bash shell. - set-x: Print commands and their arguments as they are executed. - set-v: Print shell input lines as they are read. You can use above two command in shell script itself like below: The output of running this script with bash will be. Add a variable DEBUG_ENABLE on top of your script and set it to parameter value from command line. In this example I am taking the first parameter of the script 1. Choose Your Shell. The first thing you should do before starting a shell script, or any kind of script or program for that matter, is enumerate the requirements and the goal of that script. Then evaluate what the best tool is to accomplish those goals. BASH may be easy to learn and write in, but it isn't always fit for the job.
http://www.cyberciti.biz/tips/debugging-shell-script.html http://www.cyberciti.biz/tips/debugging-shell-script.html http://unix.stackexchange.com/questions/155551/how-to-debug-a-bash-script http://www.cyberciti.biz/tips/debugging-shell-script.html http://linuxpoison.blogspot.com/2010/11/how-to-debug-shell-scripts.html http://askubuntu.com/questions/21136/how-to-debug-bash-script http://www.dba-oracle.com/t_weblogic_debugging_wlst.htm https://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Debugging_and_signal_handling http://linux-faq.blogspot.com/2009/05/debugging-shell-scripts-on-unixlinux.html http://mywiki.wooledge.org/BashGuide/Practices
best way to debug shell script