← Back to Bash Course | Chapter 13: Advanced Scripting | Lesson 2 of 7

Positional Parameters और shift

Positional parameters एक script या function को pass किए गए individual arguments हैं, $1, $2, आगे यूं ही numbered, और shift उन्हें नीचे move करता है ताकि $2 $1 बन जाए।
Syntax
bash
$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

bash
#!/bin/bash
set -- alpha beta gamma
echo "script/function name placeholder: $0"
echo "first: $1, second: $2, third: $3"
echo "total arguments: $#"

Arguments से Shift करते हुए

shift $1 हटाता है और हर बाकी argument को एक नीचे renumber करता है, इसलिए पुराना $2 नया $1 बन जाता है। यह एक arbitrary-length argument list को एक समय में एक item process करने का standard तरीका है।

उदाहरण: Shifting Through Arguments

bash
#!/bin/bash
set -- one two three
while [[ $# -gt 0 ]]; do
    echo "processing: $1"
    shift
done
echo "no arguments left: $#"

एक से ज़्यादा से Shift करना

shift N एक साथ पहले N positional parameters हटाता है, जो तब उपयोगी है जब एक manual argument-parsing loop में एक flag और इसकी value साथ consume करनी हो।

उदाहरण: Shifting by More Than One

bash
#!/bin/bash
set -- --name Ada extra1 extra2
if [[ $1 == "--name" ]]; then
    value=$2
    shift 2
fi
echo "parsed name: $value"
echo "remaining: $*"

Functions के अंदर Positional Parameters

Functions के पास उनके call होने के तरीके के आधार पर अपने independent positional parameters होते हैं, script के अपने $1, $2, आदि से पूरी तरह अलग, भले ही वही $1 syntax उपयोग हो।

उदाहरण: Positional Parameters Inside Functions

bash
#!/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"
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. यह भूल जाना कि $0 script का अपना नाम है, पहला argument नहीं; पहला असली argument $1 है।
  2. "$@" चाहिए था वहां $* उपयोग करना, word splitting या quoting अलग होने पर separate arguments के बीच का distinction खोते हुए।
  3. बाकी arguments से ज़्यादा बार shift call करना, जो आमतौर पर harmless है पर एक loop-counting bug का sign हो सकता है।
चैप्टर सारांश
  • $0 script का नाम है, $1, $2, ... actual arguments हैं, और $# argument count है।
  • shift (या shift N) पहला (या पहले N) positional parameters discard करता है, बाकी को renumber करते हुए।
  • एक while [[ $# -gt 0 ]]; do ...; shift; done loop arguments की एक unknown संख्या को एक समय में एक process करने का standard तरीका है।
  • एक function के अंदर, positional parameters function के अपने arguments refer करते हैं, script के नहीं।

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.