Function Definition in Shell

Level 375 keystrokes

Defines a function greet that keeps its first argument in a local variable and prints a greeting, then calls it with "Developer".

Shell code

# Define a function named greet
1greet() {
# $1 is the first argument; local keeps name inside the function
2 local name="$1"
# Print the greeting: Hello, Developer!
3 echo "Hello, $name!"
# return sets the exit status (0 = success), not a value
4 return 0
5}
# Call the function like a command, passing "Developer" as $1
6greet "Developer"

Output

Hello, Developer!
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