Linux Shell Script Series Tutorial (vii): script debugging

Source: Internet
Author: User

This article mainly introduced the Linux Shell Script Series tutorial (Seven): script debugging, this article explained the bash built-in debugging function and the custom debugging function and so on content, needs the friend may refer to under

First, debug scripts

Debugging is one of the important features that every programming language should implement, and when something unexpected happens, use it to generate the running information of the script, and the debugging information can help you figure out what caused the program to crash or behave abnormally.

Second, bash built-in debugging function

With the built-in debugging capabilities of bash, you can debug the entire script or only some of the statements in the script.

#使用set-X and set +x to debug a sentence of a script

The code is as follows:


For i in {1 2 3 4 5 6};


Set-x #开启调试功能

echo $i #要调试的语句

Set +x #关闭调试功能


echo "Script executed."

The code is as follows:

#使用-x option to debug the entire script

Bash-x #等价于sh-X

Three, custom debugging function

Bash's built-in debugging capabilities only output fixed-format debugging information, but in many cases we need to display debugging information in a custom format, which can be built by _DEBUG environment variables.

The code is as follows:

#使用_DEBUG =on Bash run the following script


function DEBUG ()


["$_debug" = "on"] && $@ | | : #使用_DEBUG环境变量调试


For i in {1 2 3}


DEBUG Echo $i


We are in front of every statement that needs to print debug information, and debug information will not print if _debug=on is not passed a script. In bash,: Tell the shell not to do anything.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.