विशेष Variables ($0 $1 $?)
Special variables एक package पर name tag और delivery slip जैसे हैं: एक आपको script का अपना नाम बताता है, कुछ उसे दिए गए items रखते हैं, और एक आपको बताता है कि last job ठीक से हुआ या नहीं।
In this page:
Syntax
$0 # script name
$1 # first argument
$# # argument count
$? # exit status of last command
Special Variables ($0 $1 $?)
$0 script का अपना नाम है, $1, $2, आदि script को pass किए गए positional arguments हैं, और $? सबसे recently चले command का exit status रखता है।
$# arguments की संख्या देता है, और $@ उन सबमें expand होता है। ये special variables ऐसे scripts लिखने के लिए essential हैं जो input accept करते हैं और errors जांचते हैं।
Note:
हमेशा उस command के ठीक बाद
$? जांचें जिसकी आपको परवाह है — यह हर अगले command से overwrite हो जाता है।उदाहरण: Special Variables ($0 $1 $?)
#!/bin/bash
echo "Script name: $0"
echo "First arg: $1"
echo "Arg count: $#"
true
echo "Exit status of true: $?"
false
echo "Exit status of false: $?"
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. #}
आम गलतियां
$?का उपयोग किसी दूसरे command के चलने के बाद करना, इसलिए यह गलत exit status रखता है।"$@"quote करना भूल जाना, इसलिए spaces वाले arguments split हो जाते हैं।- यह उम्मीद करना कि
$0हमेशा script का नाम होगा, जब इसमें call करने के लिए उपयोग किया गया path शामिल हो सकता है।
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: