...

/

Functions

Functions

This lesson discusses the four types of a ‘command’ that exist in bash: functions, aliases, programs, and built-ins.

From one angle, bash can be viewed as a programming language, albeit a quite slow and primitive one.

One of the language features it has is the capability to create and call functions.

This leads us onto the topic of what a ‘command’ can be in bash, and we cover all four of them: functions, aliases, programs, and builtins.

By the end you will have a more nuanced understanding of commands in bash.

How Important is this Lesson?

It’s possible to get by without knowing much about functions in bash, but any serious bash user will know what they do and how they work.

Basic Functions

Start by creating a simple function:

Press + to interact
function myfunc {
echo Hello World
}
myfunc
Terminal 1
Terminal
Loading...

Note: continue to use this terminal during this lesson. Throughout this course it is assumed that you complete each lesson by typing in the commands in the provided terminal for that lesson in the order the commands are given.

By declaring a function, and placing the block of code that needs to run inside the curly braces, you can call that function on the command line as though it were a program.

Arguments

Unlike other languages, in bash there is no checking of functions’ arguments.

Predict the output of this, and then run ...

Access this course and 1400+ top-rated courses and projects.