How do I debug a bash script?

How do I debug a bash script?

Bash provides extensive debugging features. The most common is to start up the subshell with the -x option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.

How do you debug a function in shell script?

Bash shell offers debugging options which can be turn on or off using the 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.

How do I run a debug script in Linux?

Methods of Enabling Shell Script Debugging Mode

  1. -v (short for verbose) – tells the shell to show all lines in a script while they are read, it activates verbose mode.
  2. -n (short for noexec or no ecxecution) – instructs the shell read all the commands, however doesn’t execute them.

How do I run a debug script in Unix?

Start your bash script with bash -x ./script.sh or add in your script set -x to see debug output. You can use option -p of logger command to set an individual facility and level to write output via local syslog to its own logfile.

Is there a way to debug a bash script?

Alternatively, you can use the shell verbose option instead by using set -o verbose or set -v. The main difference with xtrace is that the verbose mode will only display the shell input lines as they are read. You will not see the arguments passed to the commands which is usually helpful when trying to debug a bash script.

What to do if you get syntax error in Bash?

Let’s take a simple but common syntax error, with the incorrect use of the single square bracket and a missing then keyword. There is two options in Bash to catch this kind of syntax errors without executing the script. First, by using the shell noexec option. Second, by using a static analysis tool.

Which is the best tool for debugging Bash?

Some basic tools like using an editor with syntax highlighting will help as well. There are builtin options that Bash provides to make debugging and your everyday Linux System Administration job easier. The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.

What’s the best way to run a bash script?

The first option to mention is probably the most useful too – the xtrace option. This can be applied to a script by invoking Bash with the -x switch. Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.