Function के Arguments
Function arguments वे ingredients जैसे हैं जो आप एक recipe को देते हैं। Function के अंदर आप number से refer करते हैं कि आपको पहली, दूसरी और तीसरी चीज़ क्या दी गई थी।
In this page:
Syntax
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
#!/bin/bash
greet() {
echo "Hello, $1! You are $2 years old."
}
greet "Alice" 30
Login to try C/C++/Java/PHP code in the editor
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उपयोग करना और names की उम्मीद करना, जब arguments positional हैं।"$@"के आस-पास quotes भूल जाना, इसलिए spaces वाले arguments split हो जाते हैं।- यह उम्मीद करना कि function के अंदर
$0function का नाम होगा।
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null