← Back to Bash Course | Chapter 2: Variables & Types | Lesson 11 of 13

विशेष Variables ($0 $1 $?)

Special variables एक package पर name tag और delivery slip जैसे हैं: एक आपको script का अपना नाम बताता है, कुछ उसे दिए गए items रखते हैं, और एक आपको बताता है कि last job ठीक से हुआ या नहीं।
Syntax
bash
$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 $?)

bash
#!/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: $?"
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. $? का उपयोग किसी दूसरे command के चलने के बाद करना, इसलिए यह गलत exit status रखता है।
  2. "$@" quote करना भूल जाना, इसलिए spaces वाले arguments split हो जाते हैं।
  3. यह उम्मीद करना कि $0 हमेशा script का नाम होगा, जब इसमें call करने के लिए उपयोग किया गया path शामिल हो सकता है।

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.