AnyCompiler

Functions

A function is defined with name() { ... }. Arguments passed to it are available inside as $1, $2, and so on — the same positional-parameter syntax a whole script itself would use for its own command-line arguments.

greet() {
  echo "Hello, $1!"
}
greet "Bash"
Output
Hello, Bash!

Next: function return values — why a function's return keyword can't give you a real value back, and the command-substitution trick that actually does.

Practice exercise

Write a function named greet that takes one argument and prints "Hi, " followed by that argument and "!", then call greet "Bash".