Function Definition in Shell

Level 362 keystrokes

Functions group commands together and can accept parameters.

Shell code

# Defines a function named greet; its arguments arrive as $1, $2, ...
1greet() {
# local keeps name inside the function; $1 is the first argument
2 local name="$1"
3 echo "Hello, $name!"
4}
# Calls the function like a command, with World as $1: prints Hello, World!
5greet "World"

Output

Hello, World!
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • "× 6
  • $× 2
  • ,× 1
  • !× 1
  • (× 1
  • )× 1
  • {× 1
  • }× 1
  • =× 1

More Shell examples at level 3

← Shell typing practice