Arguments के Arrays ("$@" vs "$*" का Quoting अंतर)
In this page:
"$@" # each argument stays separate
"$*" # all arguments joined into one string
function_name "$@"
Quoted "$@" Separate Arguments सुरक्षित रखता है
"$@" हर positional parameter को इसके अपने distinct, quoted word के रूप में expand करता है, इसलिए spaces वाला एक argument iterate या forward होते समय एक single item के रूप में intact रहता है।
उदाहरण: Quoted "$@" Preserves Separate Arguments
#!/bin/bash
set -- "first arg" "second arg"
count=0
for a in "$@"; do
count=$((count + 1))
echo "arg $count: [$a]"
done
Login to try C/C++/Java/PHP code in the editor
Quoted "$*" एक String में Join करता है
"$*" सभी positional parameters को एक single string में combine करता है, IFS के पहले character से separated (default रूप से एक space), arguments के बीच original boundaries खोते हुए।
उदाहरण: Quoted "$*" Joins Into One String
#!/bin/bash
set -- "first arg" "second arg"
combined="$*"
echo "combined into one string: [$combined]"
Login to try C/C++/Java/PHP code in the editor
Arguments को किसी और Function में Forward करना
जब एक function को अपने arguments किसी और command को बिल्कुल वैसे forward करने हों जैसे received हुए, "$@" सही choice है क्योंकि यह हर argument के original boundaries सुरक्षित रखता है, embedded spaces के साथ भी।
"$@" के बजाय $* (या unquoted $@) से forward करना spaces वाले arguments को silently merge या re-split कर सकता है।उदाहरण: Forwarding Arguments to Another Function
#!/bin/bash
show_each() {
for a in "$@"; do
echo "received: [$a]"
done
}
forward_args() {
show_each "$@"
}
forward_args "has space" "nospace"
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
"$@"और"$*"same तरह behave करते हैं; unquoted वे similar हैं, पर quoted वे fundamentally अलग हैं। - दूसरे command को arguments forward करने के लिए
$*(unquoted या quoted) उपयोग करना, जो spaces वाले arguments merge या mis-split कर सकता है। - यह भूल जाना कि
"$@"हर positional parameter को अपने separately-quoted word के रूप में expand करता है, जो arguments forward करते समय almost हमेशा चाहिए होता है।
"$@"हर positional parameter को एक separate, individually-quoted word के रूप में expand करता है — arguments forward करने के लिए safe default।"$*"सभी positional parameters को एक SINGLE string में joined expand करता है,IFSके पहले character से separated।- Unquoted
$@और$*almost identically behave करते हैं (दोनों whitespace पर split होते हैं), इसलिए difference सिर्फ quoted होने पर really मायने रखता है। - जब arguments किसी और command में pass कर रहे हों
"$@"उपयोग करें;"$*"तब के लिए रखें जब आपको deliberately एक combined string चाहिए।
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