← Back to Bash Course | Chapter 12: Error Handling & Debugging | Lesson 9 of 12

Debug Mode (set -x) का उपयोग

set -x slow-motion replay जैसा है: shell हर step दिखाता है जो यह करने जा रहा है, real values के साथ भरा हुआ, ताकि आप देख सकें कि एक script कहां गलत हो जाती है।

In this page:

  1. Debug Mode (set -x)
Syntax
bash
set -x
commands
set +x

Debug Mode (set -x)

set -x Bash को हर command उसके expanded arguments के साथ run करने से पहले print करने देता है, जो exactly track करने के लिए invaluable है कि एक script क्या करती है।

set +x इसे वापस बंद कर देता है। Debug tracing आमतौर पर पूरी बड़ी script के लिए नहीं बल्कि एक suspicious section के आस-पास temporarily enable किया जाता है।

Note: पूरी script trace करने के बजाय सिर्फ suspicious section को set -x / set +x में wrap करें, output readable रखने के लिए।

उदाहरण: Debug Mode (set -x)

bash
#!/bin/bash
echo "Before debug mode"

set -x
x=5
y=$((x * 2))
set +x

echo "After debug mode, y=$y"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Production में set -x चालू छोड़ देना, जो secrets print करता है।
  2. इसे set +x से बंद करना भूल जाना।
  3. यह न जानना कि debug output standard error पर जाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.