Positional Parameters और shift
In this page:
$1 $2 $3 # positional parameters
$# # number of parameters
shift [n] # discard the first n parameters
Basic Positional Parameters
$1, $2, आगे यूं ही script (या current function) को pass किए गए arguments refer करते हैं, जबकि $0 हमेशा script का अपना नाम है और $# arguments की total count है।
उदाहरण: Basic Positional Parameters
#!/bin/bash
set -- alpha beta gamma
echo "script/function name placeholder: $0"
echo "first: $1, second: $2, third: $3"
echo "total arguments: $#"
Login to try C/C++/Java/PHP code in the editor
Arguments से Shift करते हुए
shift $1 हटाता है और हर बाकी argument को एक नीचे renumber करता है, इसलिए पुराना $2 नया $1 बन जाता है। यह एक arbitrary-length argument list को एक समय में एक item process करने का standard तरीका है।
उदाहरण: Shifting Through Arguments
#!/bin/bash
set -- one two three
while [[ $# -gt 0 ]]; do
echo "processing: $1"
shift
done
echo "no arguments left: $#"
Login to try C/C++/Java/PHP code in the editor
एक से ज़्यादा से Shift करना
shift N एक साथ पहले N positional parameters हटाता है, जो तब उपयोगी है जब एक manual argument-parsing loop में एक flag और इसकी value साथ consume करनी हो।
उदाहरण: Shifting by More Than One
#!/bin/bash
set -- --name Ada extra1 extra2
if [[ $1 == "--name" ]]; then
value=$2
shift 2
fi
echo "parsed name: $value"
echo "remaining: $*"
Login to try C/C++/Java/PHP code in the editor
Functions के अंदर Positional Parameters
Functions के पास उनके call होने के तरीके के आधार पर अपने independent positional parameters होते हैं, script के अपने $1, $2, आदि से पूरी तरह अलग, भले ही वही $1 syntax उपयोग हो।
उदाहरण: Positional Parameters Inside Functions
#!/bin/bash
show_args() {
echo "inside function: $1 $2"
}
set -- outer1 outer2
echo "outer script args: $1 $2"
show_args inner1 inner2
echo "outer script args unchanged: $1 $2"
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
$0script का अपना नाम है, पहला argument नहीं; पहला असली argument$1है। "$@"चाहिए था वहां$*उपयोग करना, word splitting या quoting अलग होने पर separate arguments के बीच का distinction खोते हुए।- बाकी arguments से ज़्यादा बार
shiftcall करना, जो आमतौर पर harmless है पर एक loop-counting bug का sign हो सकता है।
$0script का नाम है,$1,$2, ... actual arguments हैं, और$#argument count है।shift(याshift N) पहला (या पहले N) positional parameters discard करता है, बाकी को renumber करते हुए।- एक
while [[ $# -gt 0 ]]; do ...; shift; doneloop arguments की एक unknown संख्या को एक समय में एक process करने का standard तरीका है। - एक function के अंदर, positional parameters function के अपने arguments refer करते हैं, script के नहीं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling