Shell Functions & Reusable Snippets
Define and use Bash functions to package commands into reusable, parameterized building blocks for your shell.
Why Shell Functions?
A shell function groups a set of commands under a single name so you can reuse them without retyping.
- Faster than copy-pasting commands
- Cleaner than long aliases
- Can accept arguments, unlike aliases
mkcd() {
mkdir -p "$1" && cd "$1"
}Defining a Function
The simplest form uses the name() { ... } syntax. Commands inside the braces run when you call the function by name.
greet() {
echo "Hello from a function"
}
greetAll lessons in this course
- Job Control (bg, fg, jobs)
- Shell Expansion & Globbing
- Aliases and Custom Prompts
- Shell Functions & Reusable Snippets