Debug Mode (set -x) का उपयोग
set -x slow-motion replay जैसा है: shell हर step दिखाता है जो यह करने जा रहा है, real values के साथ भरा हुआ, ताकि आप देख सकें कि एक script कहां गलत हो जाती है।
In this page:
Syntax
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)
#!/bin/bash
echo "Before debug mode"
set -x
x=5
y=$((x * 2))
set +x
echo "After debug mode, y=$y"
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Production में
set -xचालू छोड़ देना, जो secrets print करता है। - इसे
set +xसे बंद करना भूल जाना। - यह न जानना कि debug output standard error पर जाता है।
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Exit Codes and Checking $? Correctly
- set -e and set -u
- Error Handling
- set -e (Exit on Error) and Its Gotchas
- Logging
- set -u (Undefined Variables) and set -x (Trace Mode)
- Script Arguments with getopts
- trap ERR for Custom Error Handling
- Debug Mode (set -x)
- Writing a Custom Error/die Function
- Portability
- Validating Script Input and Arguments