Monday, January 13, 2025
HomeQ&APassing parameters to a Bash function

Passing parameters to a Bash function

In Bash, you can pass information to a function when you call it. These inputs are called parameters, and the function can access them using $1, $2, and so on. Here’s an example:

greet_user() {
echo “Hello, $1! You are $2 years old.”
}

See also  What is 40 kilometers per hour equivalent to in other units of speed?

greet_user “Alice” 25

When you run this, the function replaces $1 with “Alice” and $2 with 25, then prints:
Hello, Alice! You are 25 years old.

You can also use $# to see how many parameters were passed and $@ to get all parameters.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x