← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 3 of 13

Function के Arguments

Function arguments वे ingredients जैसे हैं जो आप एक recipe को देते हैं। Function के अंदर आप number से refer करते हैं कि आपको पहली, दूसरी और तीसरी चीज़ क्या दी गई थी।

In this page:

  1. Function Arguments
Syntax
bash
function_name() {
  echo "$1" "$2"    # arguments
  echo "$#"         # argument count
}
function_name arg1 arg2

Function Arguments

Functions arguments ऐसे receive करते हैं जैसे scripts करती हैं: individual arguments के लिए $1, $2, आदि, count के लिए $#, और इन सबके लिए $@।

दूसरी languages जैसी कोई separate parameter-declaration syntax नहीं है — arguments simply function body के अंदर positionally पढ़े जाते हैं। Function का अपना local $1 function चलने के दौरान script के अपने $1 को shadow करता है।

Note: एक function के अंदर, $1 function के अपने पहले argument को refer करता है, script के नहीं।

उदाहरण: Function Arguments

bash
#!/bin/bash
greet() {
    echo "Hello, $1! You are $2 years old."
}

greet "Alice" 30
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. $1 उपयोग करना और names की उम्मीद करना, जब arguments positional हैं।
  2. "$@" के आस-पास quotes भूल जाना, इसलिए spaces वाले arguments split हो जाते हैं।
  3. यह उम्मीद करना कि function के अंदर $0 function का नाम होगा।

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.