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

विशेष Variables

Bash अपने आप कुछ special variables को script के बारे में और यह कैसे call किया गया इसके बारे में useful information से भर देता है, बिना आपको कभी उन्हें assign किए।
Syntax
bash
$0        # script name
$1 $2     # positional arguments
$#        # number of arguments
"$@"      # all arguments, each quoted separately
"$*"      # all arguments as one string

$0, $1, $2... और $#

$0 वह नाम रखता है जिससे script invoke किया गया था, जबकि $1, $2, और इसी तरह आगे individual arguments रखते हैं जो इसे order में pass किए गए। $# आपको बताता है कि कितने positional arguments supply किए गए थे, जो input उपयोग करने से पहले उसे validate करने के लिए useful है।

Warning: set -- alpha beta gamma यहां script के अंदर command-line arguments को demonstration के लिए simulate करने के लिए उपयोग किया गया है।

उदाहरण: $0, $1, $2... and $#

bash
#!/bin/bash
set -- alpha beta gamma
echo "Script name: $0"
echo "First arg: $1, second arg: $2"
echo "Total args: $#"

$@ बनाम $* बिना Quote किए

बिना quote किए, दोनों $@ और $* spaces से separated सभी positional parameters में expand होते हैं और फिर word splitting के subject होते हैं, इसलिए वे उस form में identically behave करते हैं। Difference सिर्फ तब दिखता है जब आप उन्हें quote करते हैं।

उदाहरण: $@ vs $* Unquoted

bash
#!/bin/bash
set -- one two three
for arg in $@; do
    echo "unquoted \$@: $arg"
done

"$@" बनाम "$*" Quote किए हुए

"$@" के रूप में quote किया जाए, तो हर positional parameter अपने खुद के separate, पूरी तरह preserved word के रूप में expand होता है (एक argument के अंदर spaces intact रहते हैं)।

"$*" के रूप में quote किया जाए, तो सभी parameters IFS के पहले character (default रूप से एक space) से separated एक single string में जुड़ जाते हैं।

Warning: किसी दूसरे command को arguments forward करते समय, "$@" almost हमेशा correct choice है।

उदाहरण: "$@" vs "$*" Quoted

bash
#!/bin/bash
set -- "first arg" "second arg"
echo "Using \"\$@\":"
for arg in "$@"; do
    echo "  [$arg]"
done
echo "Using \"\$*\": [$*]"

$?, $$, और दूसरे Specials

$? सबसे recently complete हुए command का exit status देता है, और $$ currently running shell की खुद की process ID देता है, जो अक्सर unique temporary filenames बनाने के लिए उपयोग किया जाता है।

ये specials read-only हैं और script चलते समय अपने आप update होते हैं।

उदाहरण: $?, $$, and Other Specials

bash
#!/bin/bash
echo "This script's PID is $$"
true
echo "Exit status after true: $?"
tmpfile="/tmp/scratch_$$.txt"
echo "Would use temp file: $tmpfile"
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. $@ और $* में confuse होना; बिना quote किए वे same behave करते हैं, लेकिन quote किए जाने पर, "$@" हर argument को एक separate word के रूप में expand करता है (हर एक के अंदर spaces preserve करते हुए), जबकि "$*" सभी arguments को एक single string में जोड़ देता है।
  2. यह मान लेना कि $0 हमेशा सिर्फ script का filename होता है; यह असल में वह string है जो script invoke करने के लिए उपयोग की गई, जो यह call किए जाने के तरीके पर depend करते हुए एक relative path, absolute path, या बस एक नाम हो सकती है।
  3. यह भूल जाना कि $$ current shell की अपनी process ID है, last background command start हुई की ID नहीं (वह $! है, एक related लेकिन अलग special variable)।
चैप्टर सारांश
  • $0 वह नाम/path है जो script invoke करने के लिए उपयोग किया गया; $1, $2, ... इसे pass किए गए positional arguments हैं।
  • $# positional arguments की count है; $@ और $* दोनों उन सबमें expand होते हैं, लेकिन quote किए जाने पर अलग होते हैं।
  • "$@" almost हमेशा वही है जो आप arguments forward करते समय चाहते हैं, क्योंकि यह हर argument को एक separate word के रूप में preserve करता है भले ही इसमें spaces हों।
  • $? last command का exit status है; $$ current process का PID है।

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.