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

Arguments के Arrays ("$@" vs "$*" का Quoting अंतर)

एक script के सभी arguments साथ expand करने के दो तरीके हैं, और एक हर argument को separate रखता है जबकि दूसरा उन सभी को एक single piece of text में glue कर देता है।
Syntax
bash
"$@"    # 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

bash
#!/bin/bash
set -- "first arg" "second arg"
count=0
for a in "$@"; do
    count=$((count + 1))
    echo "arg $count: [$a]"
done

Quoted "$*" एक String में Join करता है

"$*" सभी positional parameters को एक single string में combine करता है, IFS के पहले character से separated (default रूप से एक space), arguments के बीच original boundaries खोते हुए।

उदाहरण: Quoted "$*" Joins Into One String

bash
#!/bin/bash
set -- "first arg" "second arg"
combined="$*"
echo "combined into one string: [$combined]"

Arguments को किसी और Function में Forward करना

जब एक function को अपने arguments किसी और command को बिल्कुल वैसे forward करने हों जैसे received हुए, "$@" सही choice है क्योंकि यह हर argument के original boundaries सुरक्षित रखता है, embedded spaces के साथ भी।

Warning: "$@" के बजाय $* (या unquoted $@) से forward करना spaces वाले arguments को silently merge या re-split कर सकता है।

उदाहरण: Forwarding Arguments to Another Function

bash
#!/bin/bash
show_each() {
    for a in "$@"; do
        echo "received: [$a]"
    done
}

forward_args() {
    show_each "$@"
}

forward_args "has space" "nospace"
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. यह मान लेना कि "$@" और "$*" same तरह behave करते हैं; unquoted वे similar हैं, पर quoted वे fundamentally अलग हैं।
  2. दूसरे command को arguments forward करने के लिए $* (unquoted या quoted) उपयोग करना, जो spaces वाले arguments merge या mis-split कर सकता है।
  3. यह भूल जाना कि "$@" हर 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 चाहिए।

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.