विशेष Variables
In this page:
$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 है।
set -- alpha beta gamma यहां script के अंदर command-line arguments को demonstration के लिए simulate करने के लिए उपयोग किया गया है।उदाहरण: $0, $1, $2... and $#
#!/bin/bash
set -- alpha beta gamma
echo "Script name: $0"
echo "First arg: $1, second arg: $2"
echo "Total args: $#"
Login to try C/C++/Java/PHP code in the editor
$@ बनाम $* बिना Quote किए
बिना quote किए, दोनों $@ और $* spaces से separated सभी positional parameters में expand होते हैं और फिर word splitting के subject होते हैं, इसलिए वे उस form में identically behave करते हैं। Difference सिर्फ तब दिखता है जब आप उन्हें quote करते हैं।
उदाहरण: $@ vs $* Unquoted
#!/bin/bash
set -- one two three
for arg in $@; do
echo "unquoted \$@: $arg"
done
Login to try C/C++/Java/PHP code in the editor
"$@" बनाम "$*" Quote किए हुए
"$@" के रूप में quote किया जाए, तो हर positional parameter अपने खुद के separate, पूरी तरह preserved word के रूप में expand होता है (एक argument के अंदर spaces intact रहते हैं)।
"$*" के रूप में quote किया जाए, तो सभी parameters IFS के पहले character (default रूप से एक space) से separated एक single string में जुड़ जाते हैं।
"$@" almost हमेशा correct choice है।उदाहरण: "$@" vs "$*" Quoted
#!/bin/bash
set -- "first arg" "second arg"
echo "Using \"\$@\":"
for arg in "$@"; do
echo " [$arg]"
done
echo "Using \"\$*\": [$*]"
Login to try C/C++/Java/PHP code in the editor
$?, $$, और दूसरे Specials
$? सबसे recently complete हुए command का exit status देता है, और $$ currently running shell की खुद की process ID देता है, जो अक्सर unique temporary filenames बनाने के लिए उपयोग किया जाता है।
ये specials read-only हैं और script चलते समय अपने आप update होते हैं।
उदाहरण: $?, $$, and Other Specials
#!/bin/bash
echo "This script's PID is $$"
true
echo "Exit status after true: $?"
tmpfile="/tmp/scratch_$$.txt"
echo "Would use temp file: $tmpfile"
Login to try C/C++/Java/PHP code in the editor
$@और$*में confuse होना; बिना quote किए वे same behave करते हैं, लेकिन quote किए जाने पर,"$@"हर argument को एक separate word के रूप में expand करता है (हर एक के अंदर spaces preserve करते हुए), जबकि"$*"सभी arguments को एक single string में जोड़ देता है।- यह मान लेना कि
$0हमेशा सिर्फ script का filename होता है; यह असल में वह string है जो script invoke करने के लिए उपयोग की गई, जो यह call किए जाने के तरीके पर depend करते हुए एक relative path, absolute path, या बस एक नाम हो सकती है। - यह भूल जाना कि
$$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 है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: