set -u (Undefined Variables) और set -x (Trace Mode)
In this page:
set -u
echo "${variable:-default}"
set -x # trace commands
set +x
Catching Unset Variables
set -u active होने पर, एक ऐसी variable को reference करना जिसे कभी assign नहीं किया गया एक immediate error और non-zero exit cause करता है, जो variable names में typos को silently कुछ भी नहीं में expand होने के बजाय early catch करने में मदद करता है।
उदाहरण: Catching Unset Variables
#!/bin/bash
(
set -u
echo "$defined_var" 2>/dev/null
) ; defined_var="hello"
(
set -u
echo "now it's set: $defined_var"
)
Login to try C/C++/Java/PHP code in the editor
Safely Demonstrating set -u's Error
Failing case को एक subshell के अंदर run करना और इसके overall result को || से check करना एक script को यह दिखाने देता है कि set -u क्या catch करता है बिना उस failure को पूरा script खत्म करने दिए।
उदाहरण: Safely Demonstrating set -u's Error
#!/bin/bash
(set -u; echo "$totally_undefined_variable") 2>/dev/null || echo "caught: set -u stopped an unset variable reference"
Login to try C/C++/Java/PHP code in the editor
Providing Defaults to Avoid set -u Errors
${var:-default} हमेशा default में expand होता है जब var unset (या empty) हो, जो एक script को set -u के साथ compatible बनाने का standard तरीका है फिर भी optional variables allow करते हुए, जैसे positional parameters जो दिए न गए हों।
उदाहरण: Providing Defaults to Avoid set -u Errors
#!/bin/bash
set -u
name="${1:-anonymous}"
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Tracing Execution with set -x
set -x bash को हर command को stderr पर print करवाता है, + से prefixed, इसे run करने से ठीक पहले, variables already उनकी actual values में expanded के साथ। यह exactly देखने के सबसे तेज़ तरीकों में से एक है कि एक script क्या कर रहा है।
exec 2>trace.log से trace output को separately redirect करें।set -x passwords या tokens जैसी sensitive values print कर सकता है; secrets handle करने वाले code के आस-पास इससे बचें।उदाहरण: Tracing Execution with set -x
#!/bin/bash
set -x
num=5
double=$((num * 2))
set +x
echo "tracing disabled again; double is $double"
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
set -uएक unset variable को एक error के रूप में treat करता है even$1जैसी common चीज़ में जब कोई argument pass नहीं किया गया था, जिसे${1:-}जैसा एक default चाहिए। - Production output में
set -xenabled छोड़ देना, जो sensitive values (जैसे variables में stored passwords) को logs में leak कर सकता है। - यह मान लेना कि
set -uएक variable को catch करता है जो एक empty string में set है; empty और unset अलग हैं, औरset -uसिर्फ बाद वाले की शिकायत करता है।
set -uएक error cause करता है जब एक unset variable reference की जाती है, इसे silently एक empty string treat करने के बजाय।${var:-default}एक fallback value प्रदान करता है और एक ऐसी variable के लिए एकset -uerror से बचता है जो set न हो सकती हो।set -xहर command (variables expanded के साथ) को इसके execute होने से ठीक पहले stderr पर print करता है, जो debugging के लिए invaluable है।set +uऔरset +xहर mode को फिर से off कर देते हैं; दोनों कोset -eके साथ भी एक strict-mode header के हिस्से के रूप में combine किया जा सकता है।
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