- 01Setting up (what you'll need)
- 02Your first shell script (echo)
- 03Using variables
- 04Command-line arguments ($1, $2 ...)
- 05Branching (the if statement)
- 06Repeating (the for loop)
- 07Repeating (the while loop)
- 08Writing your own function
- 09Working with arrays
- 10Command substitution ($())
- 11Working with strings (parameter expansion)
- 12Reading from standard input (read)
- 13Exit status and && / ||
- 14Pipes and redirection
- 15The case statement
- 16Arithmetic ($(( )))
- 17The basics of grep, sed and awk
- 18Argument checks and error handling
- 19Function return values and exit codes
- 20[Project] Build a small log-tallying script
Writing your own function
This lesson covers the basic form for defining a function in a shell script.
A function is defined as name() { ... }. Arguments arrive as $1, $2, and so on — the same notation as the script's own arguments.
The sample code defines greet() { echo "Hello, $1!"; } and calls it as greet "Bob".
A common early stumble is how to return a value from a function. The return value of most languages can only carry a numeric exit status (0–255) here, so when you want a string result the usual approach is to echo it and capture it at the call site with $( ).
In professional work, gathering logic into a function in one place, rather than copying it around, is what keeps a script maintainable.
๐งช This site can't compile or run Shell Script directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
